CakePHP:验证消息未显示

Ari*_*rif 3 php cakephp

我是cakePHP的新手,我在一些教程后做了一个简单的表格.在这个html表单上,我使用了验证.现在的问题是验证工作正常但消息未显示我要显示的内容.我试试下面的代码,

模型

 public $validate = array(
        'title' => array(
            'title_required' => array(
                'rule' => 'notEmpty',
                'message' => 'This is required field'
            ),
            'title_unique' => array(
                'rule' => 'isUnique',
                'message' => 'This should be unique title'
            )
        )
    );
Run Code Online (Sandbox Code Playgroud)

调节器

public function add() {
        if ($this->request->data) {
            if ($this->Post->save($this->request->data)) {
                $this->Session->setFlash('Post has been added successfully');
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash('Error occured, Please try agan later!');
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

视图

<h2>Add New Post</h2>
<?php
echo $this->Form->create('Post', array('action'=>'add'));
echo $this->Form->input('title');
echo $this->Form->input('body');
echo $this->Form->end('Create Post');
?>
Run Code Online (Sandbox Code Playgroud)

我看到的验证错误,但它不是我在控制器中提到的消息

在此输入图像描述

Ros*_*oss 16

这是内置的浏览器验证.

从2.3开始,HTML5 required属性也将根据验证规则添加到输入中.

titlenotEmpty规则,所以Cake正在输出

<input type="text" required="required" ..

并且您的浏览器正在触发该消息.

编辑:要覆盖此行为,您可以执行以下操作:

$this->Form->input('title', array('required'=>false));
Run Code Online (Sandbox Code Playgroud)

要么

$this->Form->submit('Submit', array('formnovalidate' => true));
Run Code Online (Sandbox Code Playgroud)

提交表单时,将触发模型验证.