我正在使用cakefp(2.4.7)进行开发,但表单操作链接有问题。
我有一个带有编辑操作的usersController。
public function edit($id = null, $slug = null) {
if (!$id) {
throw new NotFoundException(__('Invalid User'));
}
$user = $this->User->findById($id);
if (!$user) {
throw new NotFoundException(__('Invalid User'));
}
if ($this->request->is(array('post', 'put'))) {
// Do stuff here
}
// Fill the form
if (!$this->request->data) {
$this->request->data = $user;
}
}
使用此代码的表单($this->create->[用户]);在编辑视图中正确填充。但我在编辑视图中有另一种形式。
类似:
echo $this->Form->create(null, array(
'url' => array('controller' => 'useraddresses', 'action' => 'add')
));
echo $this->Form->input('searchvalue');
echo $this->Form->hidden('country');
echo $this->Form->hidden('city');
echo $this->Form->end('save');
当我单击此表单中的发送按钮时,页面链接到/useraddresses/add/2(2是用户的id)我已经用firebug调试了表单,在操作参数中也是/useraddresses/add/2。
我怎么能绕过这个?我将不带任何参数地将表格发送到/useraddresses/add。
如果我在编辑操作中删除了这段代码,操作链接是正确的,但我的第一个表单没有被填写。
// Fill the form
if (!$this->request->data) {
$this->request->data = $user;
}
使用以下
if(empty($this->data) )
{
if (!$this->request->data) {
$this->request->data = $user;
}
}
而不是你的
if (!$this->request->data) {
$this->request->data = $user;
}