2

因此,验证错误会出现在我的添加操作中,但不会出现在我的编辑操作中。以下是我的控制器的片段:

在这里,我按预期收到验证错误消息:

    public function add() {
        if ($this->request->is('post')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved.'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please try again.'));
            }
        }
        $this->set('clients', $this->User->Client->find('list'));
    }

但不是在这里:

    public function edit($id = null) {
        $this->User->id = $id;
        if (!$this->User->exists()) {
            throw new NotFoundException(__('Invalid user'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->User->save($this->request->data)) {
                $this->Session->setFlash(__('The user has been saved.'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The user could not be saved. Please, try again.'));
            }
        } else {
            $this->request->data = $this->User->read(null, $id);
            unset($this->request->data['User']['password']);
        }
        $this->set('user', $this->User->read());
        $this->set('clients', $this->User->Client->find('list'));
    }
4

2 回答 2

3

如果我没记错的话,在保存失败后使用 read() 调用将清除验证错误。

那里..我发现它http://book.cakephp.org/1.3/view/1017/Retrieving-Your-Data#read-1029

于 2012-04-16T13:51:26.957 回答
0

是否触发了 setFlash('user could not be saved') 消息?

debug($this->User->validationErrors) 的输出是什么 - 在 setFlash 失败消息之后添加它

您的帖子数组是否包含需要保存的所有字段?您的 $validate 中是否有您的 post 数组中没有的必填字段?(Cake 会记录错误,但除非您的表单中有该字段,否则无法显示它)。或者另一个失败的验证规则,但您没有在表单中显示该字段?

于 2012-04-16T13:22:40.043 回答