Validation 电子邮件验证错误消息-CakePHP
我的用户模型:Validation 电子邮件验证错误消息-CakePHP,validation,cakephp-2.1,cakephp-model,Validation,Cakephp 2.1,Cakephp Model,我的用户模型: class User extends AppModel { public $validate = array( 'username' => 'alphaNumeric', 'password' => 'notempty', 'confirmpassword' => 'notempty', 'group_id' => 'notempty', 'email' => a
class User extends AppModel {
public $validate = array(
'username' => 'alphaNumeric',
'password' => 'notempty',
'confirmpassword' => 'notempty',
'group_id' => 'notempty',
'email' => array(
'rule' => 'email',
'message' => 'Please provide a valid email address.'
),
);
}
我的用户注册视图:
<div class="users form">
<?php echo $this->Form->create('User'); ?>
<fieldset>
<legend><?php echo __('Register User'); ?></legend>
<?php
echo $this->Form->input('User.username');
echo $this->Form->input('User.password');
echo $this->Form->input('User.confirmpassword', array('type'=>'password', 'label'=>'Confirm password'));
echo $this->Form->input('User.group_id');
echo $this->Form->input('User.email');
?>
</fieldset>
<?php echo $this->Form->end(__('Submit')); ?>
</div>
访问/users/register时,当我在电子邮件字段中输入类似“asdfgh”的内容时,我会得到默认的CakePHP错误消息:
Invalid email: asdfgh
Error: An Internal Error Has Occurred.
而不是电子邮件字段上方的错误消息,告诉我:
Please provide a valid email address.
我的问题:当用户的输入数据无效时,它真的应该调用控制器中的操作吗?只有当用户的输入数据有效时,才有办法调用控制器中的操作吗?我找到了答案^^
实际上,验证只在“save()”方法之前完成,而不是在控制器中的操作调用之前完成。但我可以使用以下方法验证控制器中的用户输入:
if ( $this->User->validates() )
在控制器中重新设置我的用户重新注册操作:
public function register() {
if ($this->request->is('post')) {
...
//create mail for verification
$email = new CakeEmail();
...
$email->to($this->data['User']['email']);
$email->send($ms);
}
}
public function register() {
if ($this->request->is('post')) {
$this->User->set( $this->data );
if ( $this->User->validates() ) {
...
//create mail for verification
$email = new CakeEmail();
...
$email->to($this->data['User']['email']);
$email->send($ms);
}
}
}
我想你的控制器有问题。您可以在/tmp/logs/error.log中找到有关该错误的更多信息。您是对的,问题是控制器中的一个操作,它尝试发送电子邮件,但当电子邮件无效时失败。如何在控制器中调用操作之前验证用户的电子邮件?请尝试
if(!Validation::email($email)){//Invalid email!;}