CakePHP - edit在保存时验证失败时丢失URL变量



用户从这里开始:

/admin/cuisines/edit/16

用户输入违反验证规则的内容并单击Submit。用户被占用:

/admin/cuisines/edit/16

用户认为它是有效的,并再次点击提交-然后他们被带到这里:

/admin/cuisines/edit/

用户更正了他们的错误,单击提交,而不是编辑,它将其保存为表中的新项,因为没有id。

我的代码:

    function admin_edit($id = null) { // EDIT ***********
    if (!$id && empty($this->data)) {
        $this->Session->setFlash(__('Invalid cuisine', true));
        $this->redirect(array('action' => 'index'));
    }
    if (!empty($this->data)) {
        if ($this->Cuisine->save($this->data)) {
            $this->Session->setFlash(__('The edits to this cuisine have been saved', true));
            $this->redirect(array('action' => 'index'));
        } else {
            $this->Session->setFlash(__('The edits to this cuisine could not be saved. Please, try again.', true));
        }
    }
    if (empty($this->data)) {
        $this->data = $this->Cuisine->read(null, $id);
    }
}

你知道我做错了什么吗?我以为我做的就像教程一样,但是-我不得不猜测蛋糕足够聪明,不会让这种情况发生-即-我的假设是我做错了什么

为避免在提交编辑表单时保存新项,请确保存在id的表单字段。

<?php echo $this->Form->input('id'); ?>

该字段将被自动隐藏,因为它是模型的主键。

还要检查HTML中的form action属性。我有时不得不手动设置,以避免出错。

<?php echo $this->Form->create('Cuisine', array(
  'url' => array(
    'action' => 'edit',
    $this->Form->value('id')
   )
)); ?>

最新更新