CakePHP:为什么不出现验证错误? [英] CakePHP: why aren't validation errors showing up?

查看:74
本文介绍了CakePHP:为什么不出现验证错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So, validation errors show up in my add action, but not in my edit action. Here are the snippets from my controller:

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

Here I get validation error messages as expected:

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


推荐答案

在失败的保存后的read()调用将清除验证错误。

If i recall correctly, using the read() call after a failed save will clear the validation errors.

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

there.. i found it http://book.cakephp.org/1.3/view/1017/Retrieving-Your-Data#read-1029

这篇关于CakePHP:为什么不出现验证错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆