如何显示cakephp 2.5.3模型验证消息。默认情况下,它显示HTML5验证消息

如何显示cakephp 2.5.3模型验证消息。默认情况下,它显示HTML5验证消息,html,cakephp,Html,Cakephp,如何显示CakePHP模型中定义的验证消息。当我提交表单时,CakePHP模型验证工作。但是输入字段的验证消息不显示。它显示HTML5验证消息。我想显示CakePHP模型验证消息。我正在使用CakePHP2.5.3 以下是我的代码块: 控制器: $this->User->set($this->request->data); if ($this->User->validates()) { if ($this->User->sa

如何显示CakePHP模型中定义的验证消息。当我提交表单时,CakePHP模型验证工作。但是输入字段的验证消息不显示。它显示HTML5验证消息。我想显示CakePHP模型验证消息。我正在使用CakePHP2.5.3

以下是我的代码块:

控制器:

$this->User->set($this->request->data);
    if ($this->User->validates()) {
        if ($this->User->save($this->request->data)) {
            $Email->send();
            $this->Session->setFlash(__('The user has been created'));
            if ($this->Auth->login()) {

                $this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
                $this->redirect($this->Auth->redirectUrl());
            }
        } else {
            $this->Session->setFlash(__('The user could not be created. Please, try again.'));
        }
    }
型号:

public $validate = array(
    'username' => array(
        'nonEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'A username is required',
            'allowEmpty' => false
        ),
        'unique' => array(
            'rule' => array('isUniqueUsername'),
            'message' => 'This username is already in use'
        ),
        'alphaNumericDashUnderscore' => array(
            'rule' => array('alphaNumericDashUnderscore'),
            'message' => 'Username can only be letters, numbers and underscores'
        ),
    )
视图:


您不需要在save调用周围包装验证块

这就足够了:

    if ($this->User->save($this->request->data)) {
        $Email->send();
        $this->Session->setFlash(__('The user has been created'));
        if ($this->Auth->login()) {

            $this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
            $this->redirect($this->Auth->redirectUrl());
        }
    } else {
        $this->Session->setFlash(__('The user could not be created. Please, try again.'));
    }

CakePHP在调用Model->save时自动处理验证,并在输入字段下显示错误。因此,如果$this->save由于验证错误而失败,cakephp会将模型验证数组中的消息文本添加到右侧输入字段下。

剩下两个选项:

或者不要在客户端验证的cakePHP中使用验证规则

在DOM ready上,调用$formId.removeAttr'novalidate';像

$document.readyfunction{ $formId.removeAttr'novalidate'; };


可能的副本,请考虑使用搜索功能或谷歌下一次这种琐碎的问题。请参阅上面帖子中的链接答案。
    if ($this->User->save($this->request->data)) {
        $Email->send();
        $this->Session->setFlash(__('The user has been created'));
        if ($this->Auth->login()) {

            $this->Session->setFlash(__('Welcome, ' . $this->Auth->user('username')));
            $this->redirect($this->Auth->redirectUrl());
        }
    } else {
        $this->Session->setFlash(__('The user could not be created. Please, try again.'));
    }