Php 仅跳过表单上一列的验证
我的Cake2.4应用程序有一个密码重置表单,这给了我一些验证问题 当我尝试对用户进行简单的保存时,我遇到了一个验证错误,即电子邮件地址不是唯一的,即使我只是在更新用户的密码 当我在保存中将Php 仅跳过表单上一列的验证,php,cakephp,cakephp-2.0,cakephp-2.4,Php,Cakephp,Cakephp 2.0,Cakephp 2.4,我的Cake2.4应用程序有一个密码重置表单,这给了我一些验证问题 当我尝试对用户进行简单的保存时,我遇到了一个验证错误,即电子邮件地址不是唯一的,即使我只是在更新用户的密码 当我在保存中将$validate设置为false时,表单工作正常。但是,这将跳过所有验证规则,包括我的密码验证规则 如何进行此保存,使其不会抱怨非唯一电子邮件地址,并且仍然正确验证密码 电子邮件地址是唯一的,据我所知,我的控制器操作并没有试图更改它。在我的模型中规定“创建”不会有帮助,否则用户可以将他们的电子邮件地址设置为
$validate
设置为false时,表单工作正常。但是,这将跳过所有验证规则,包括我的密码验证规则
如何进行此保存,使其不会抱怨非唯一电子邮件地址,并且仍然正确验证密码
电子邮件地址是唯一的,据我所知,我的控制器操作并没有试图更改它。在我的模型中规定“创建”不会有帮助,否则用户可以将他们的电子邮件地址设置为其他人的
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);
}
}
public function reset\u密码($token=null){
如果($this->Session->read('Auth.User')){
$this->Session->setFlash(_uu('您已经登录,请在此处更新密码'),'flash/success');
$this->redirect(数组('action'=>'profile');
}
如果(!空($token)){
如果($user=$this->user->findBytoken($token)){
$id=$user['user']['id'];
$this->User->id=$id;
}
elseif(计数($this->User->findBytoken($token)==0)){
$this->Session->setFlash(_uu(“您的密码重置链接无效。请请求一个新链接”),“flash/error”);
$this->redirect(数组('action'=>'忘记密码');
}
如果(strotime($user['user']['token\u expires'])Session->setFlash(“为了您的安全,您的重置链接已在24小时后过期。请请求一个新链接”),“flash/error”);
$this->redirect(数组('action'=>'忘记密码');
}
}否则{
$this->Session->setFlash(_uu(“您需要一个重置链接来完成此操作。在此处请求一个带有您的电子邮件地址的链接”),“flash/success”);
$this->redirect(数组('action'=>'忘记密码');
}
如果(!$this->User->exists($this->User->id)){
抛出新的NotFoundException(uuu('Invalid user'));
}
如果($this->request->is('post')| |$this->request->is('put')){
$this->request->data['User']['token']=null;
$this->request->data['User']['token\u expires']=null;
$this->request->data['User']['modified']=false;
取消设置($this->request->data['User']['image']);
如果($this->User->save($this->request->data,$validate=false)){
$this->Session->setFlash(_uu(“您的密码已更新。您现在可以使用新密码登录”),“flash/success”);
$this->redirect(数组('action'=>'login');
}否则{
$options=array('conditions'=>array('User'.$this->User->primaryKey=>$id));
$this->set('user',$this->user->find('first',$options));
$this->Session->setFlash(_uu(“您的密码无法更新。请重试”),“flash/error”);
}
}否则{
$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);
}
}
来自CakePHP文档
“on”键可设置为以下任一值:
“更新”或“创建”。这提供了一种机制,允许特定的
在创建新记录期间应用的规则,或
在更新记录期间
如果规则定义了“on'=>“create”,则该规则将仅强制执行
在创建新记录的过程中。同样,如果定义为
'on'=>'update',仅在更新
记录
或者,在控制器中,您可以仅关闭电子邮件字段的验证,然后不将其包含在要保存的数据中
$this->User->validator()->remove('email');
unset($this->request->data['User']['email']);
另外,我强烈推荐是,但是我需要在创建和更新时使用isUnique规则。否则,用户可以在创建帐户后将其电子邮件地址更新为无效的(即其他人的)。每个字段可以有多个规则。我更新了我的答案,以显示如何
$this->User->validator()->删除('email');取消设置($this->request->data['User']['email'])代码>成功了!
$this->User->validator()->remove('email');
unset($this->request->data['User']['email']);