我是 cakePHP 的新手,我按照一些教程制作了一个简单的表单。在这个 html 表单上我使用了验证。现在的问题是验证正在工作,但消息没有显示我希望它显示的内容。我尝试了下面的代码。
Model
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!');
}
}
}
View
<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');
?>
我看到的验证错误不是我在控制器中提到的消息。
这是内置的浏览器验证。
从 2.3 开始,HTML5 required 属性也将根据验证规则添加到输入中。
Your title
有notEmpty
规则,所以 Cake 正在输出
<input type="text" required="required" ..
并且您的浏览器正在触发该消息。
Edit:要覆盖此行为,您可以执行以下操作:
$this->Form->input('title', array('required'=>false));
or
$this->Form->submit('Submit', array('formnovalidate' => true));
当您提交表单时,您的模型验证将被触发。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)