CakePHP:为什么不出现验证错误?

时间:2012-04-16 12:54:32

标签: validation cakephp

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

这里我按预期得到验证错误消息:

    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'));
    }

2 个答案:

答案 0 :(得分:3)

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

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

答案 1 :(得分:0)

是否触发了setFlash('用户无法保存')消息?

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

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