我的Cake2.4应用程序有一个密码重置表单,这给我一些验证问题。
当我尝试对用户进行简单保存时,我遇到验证错误,即电子邮件地址不是唯一的 - 即使我只是更新了用户的密码。
当我在保存中将$validate
设置为false时,表单工作正常。但是,这会跳过所有验证规则,包括我的密码验证规则。
如何保存,以便它不会抱怨非唯一的电子邮件地址,并且仍能正确验证密码?
电子邮件地址是唯一的,据我所知,我的控制器操作并没有尝试更改它。制定' on create'在我的模型中没有帮助 - 否则用户可以将他们的电子邮件地址设置给其他人。
public function reset_password($token = null) {
if ($this->Session->read('Auth.User')) {
$this->Session->setFlash(__('You\'re already logged in. Update your password here.'), 'flash/success');
$this->redirect(array('action' => 'profile'));
}
if (!empty($token)){
if ($user = $this->User->findBytoken($token)){
$id = $user['User']['id'];
$this->User->id = $id;
}
elseif (count($this->User->findBytoken($token) == 0)){
$this->Session->setFlash(__('Your password reset link was invalid. Please request a new one'), 'flash/error');
$this->redirect(array('action' => 'forgot_password'));
}
if (strtotime($user['User']['token_expires']) < strtotime('now')){
$this->Session->setFlash(__('For your safety, your reset link has expired after 24 hours. Please request a new one'), 'flash/error');
$this->redirect(array('action' => 'forgot_password'));
}
} else {
$this->Session->setFlash(__('You need a reset link to do that. Request one with your email address here.'), 'flash/success');
$this->redirect(array('action' => 'forgot_password'));
}
if (!$this->User->exists($this->User->id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
$this->request->data['User']['token'] = null;
$this->request->data['User']['token_expires'] = null;
$this->request->data['User']['modified'] = false;
unset($this->request->data['User']['image']);
if ($this->User->save($this->request->data, $validate = false)) {
$this->Session->setFlash(__('Your password has been updated. You can log in with your new password now'), 'flash/success');
$this->redirect(array('action' => 'login'));
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
$this->Session->setFlash(__('Your password could not be updated. Please, try again.'), 'flash/error');
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
$this->request->data = $this->User->find('first', $options);
}
}
答案 0 :(得分:2)
来自CakePHP文档http://book.cakephp.org/2.0/en/models/data-validation.html
'on'键可以设置为以下任一值: '更新'或'创建'。这提供了允许某种机制 在创建新记录期间应用的规则,或 在更新记录期间。
如果规则已定义'on'=&gt; '创建',规则只会被强制执行 在创建新记录期间。同样,如果定义为 'on'=&gt; '更新',它只会在更新期间强制执行 记录。
public $validate = array(
'email' => array(
'rule1' => array(
'rule' => array('email'),
'required' => true,
'allowEmpty' => false,
'message' => 'Email is invalid'
),
'rule2' => array(
'rule' => 'isUnique',
'required' => true,
'allowEmpty' => false,
'on' => 'create',
'message' => 'Email is not unique'
)
);
或者在Controller中,您可以仅关闭电子邮件字段的验证,然后不将其包含在要保存的数据中。
$this->User->validator()->remove('email');
unset($this->request->data['User']['email']);
另外,我强烈推荐PasswordableBehavior