【发布时间】:2012-11-27 16:41:16
【问题描述】:
我的用户模型:
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>
我在控制器中的用户注册操作:
public function register() {
if ($this->request->is('post')) {
...
//create mail for verification
$email = new CakeEmail();
...
$email->to($this->data['User']['email']);
$email->send($ms);
}
}
访问 /users/register,当我在电子邮件字段中输入类似“asdfgh”的内容时,我收到默认的 CakePHP 错误消息:
Invalid email: asdfgh
Error: An Internal Error Has Occurred.
而不是电子邮件字段上方的错误消息,告诉我:
Please provide a valid email address.
我的问题:当用户的输入数据无效时,它真的应该调用控制器中的动作吗?有没有办法只在用户输入数据有效的情况下才调用控制器中的动作?
【问题讨论】:
-
我认为您的控制器有问题。您可以在 /tmp/logs/error.log 中找到有关错误的更多信息
-
你是对的,问题是控制器中的一个动作,它试图发送一封电子邮件,但是当电子邮件无效时它失败了。如何在调用控制器中的操作之前验证用户的电子邮件?
-
试试
if (!Validation::email($email)) { //Invalid email!; }
标签: validation cakephp-2.1 cakephp-model