如何显示CakePHP model.When中定义的验证消息我提交了表单,CakePHP模型验证works.But输入字段的验证消息没有显示。它显示HTML5验证消息。我想要显示CakePHP模型验证消息。我使用的是CakePHP 2.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'
),
)查看:
<div id="divfirstname" class="input text required">
<label for="UserFirstname">First Name <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('firstname');?>
</div>
<div id="divlastname" class="input text required">
<label for="UserLastname">Last Name <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('lastname');?>
</div>
<div class="input text required">
<label for="UserUsername">Username <span style="color:#FF0000">*</span></label>
<?php echo $this->Form->text('username');?>
</div>发布于 2015-02-20 19:55:05
您不需要将验证块包含在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.'));
}当调用Model->save()时,CakePHP会自动处理验证,并在输入字段下显示错误。因此,如果$this->save由于验证错误而失败,cakephp会在正确的输入字段下添加<div class="error-message">Message Text from your model validation array</div>。
发布于 2015-02-20 20:24:43
你只有两个选择:
$("#formId").removeAttr('novalidate'); Like$(document).ready(function(){ $("#formId").removeAttr('novalidate'); });
https://stackoverflow.com/questions/28627876
复制相似问题