php - CakePHP : Validation message not displaying

标签 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'
            )
        )
    );

Controller

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

查看

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

我看到的验证错误不是我在 Controller 中提到的消息。

enter image description here

最佳答案

这是内置的浏览器验证。

自 2.3 起,HTML5 required 属性也将根据验证规则添加到输入中。

你的 titlenotEmpty规则,所以 Cake 正在输出

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

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

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

$this->Form->input('title', array('required'=>false));

$this->Form->submit('Submit', array('formnovalidate' => true));

当您提交表单时,您的模型验证将触发。

关于php - CakePHP : Validation message not displaying,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14723685/

相关文章:

php - 在 PHP 中使用私有(private)静态回调是否有效?

php - Laravel orderBy 排序不正确

cakePHP - 如何有条件地将组件加载到 Controller 中?

php - 修改 beforeFind 回调中所需的 Containable 字段?

php - curl 错误 18 - 传输已关闭,剩余未完成的读取数据

php - 缩略图生成在服务器上不起作用 - PHP Gallery

php - 更新帖子条款

php - CakePHP 3 - 比较密码

mysql - 使用附加条件连接表

php - cakePHP 数据插入,但应在编辑情况下更新