CakePHP:验证消息未显示 [英] CakePHP : Validation message not displaying

查看:44
本文介绍了CakePHP:验证消息未显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是CakePHP的新手,并且按照一些教程制作了一个简单的表格。在此html表单上,我使用了验证。现在的问题是,验证工作正常,但消息未显示我想要显示的内容。我尝试了下面的代码。

I'm new to cakePHP and I've made a simple form following some tutorial. On this html form I've used validation. Now the problem is that the validation is working but the message is not displaying what I want it to display. I tried the code below.

模型

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

控制器

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

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

The validation error which I've seen is not the message I mentioned in my controller.

推荐答案

这是内置的浏览器验证。

That's built-in browser validation.

从2.3开始,HTML5 required属性还将基于验证规则添加到输入中。

Since 2.3 the HTML5 required attribute will also be added to the input based on validation rules.

您的标题具有 notEmpty 规则,因此Cake正在输出

Your title has the notEmpty rule, so Cake is outputting

<输入类型= text required =必需 ..

,而您的浏览器正在触发该消息。

and your browser is triggering that message.

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

to override this behaviour, you can do:

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

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

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

When you submit the form, your model validation will fire.

这篇关于CakePHP:验证消息未显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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