[英]Reset Password Doesn't Save New Password CakePHP 2.0
我现在已经使系统通过了该过程,但是由于某种原因,用于实际更新数据库中密码的代码没有更新任何内容吗? 插入新密码并重新输入密码后,即使没有显示错误,系统也会立即重定向到登录页面。 有什么想法吗? 谢谢。
这是我的重置功能
function reset($token=null){
$this->User->recursive=-1;
if(!empty($token)){
$u=$this->User->findBytokenhash($token);
if($u){
$this->User->id=$u['User']['id'];
if($this->request->is('post')){
$this->User->data=$this->request->data;
$this->User->data['User']['username']=$u['User']['username'];
$new_hash=sha1($u['User']['username'].rand(0,100));//created token
$this->User->data['User']['tokenhash']=$new_hash;
// print_r($this->request->data);
// exit;
if($this->User->validates(array('fieldList'=>array('password','password_confirm')))){
if($this->User->save($this->User->data)){
$this->Session->setFlash('Password Has been Updated');
$this->redirect(array('controller'=>'users','action'=>'login'));
}
} else{
$this->set('errors',$this->User->invalidFields());
}
}
} else {
$this->Session->setFlash('Token Corrupted,,Please Retry.the reset link work only for once.');
}
} else{
$this->redirect('/');
}
}
这是我的reset.ctp
<?php echo $this->Form->create('User', array('action' => 'reset')); ?>
<?php
if(isset($errors)){
echo '<div class="error">';
echo "<ul>";
foreach($errors as $error){
echo "<li><div class='error-message'>$error</div></li>";
}
echo "</ul>";
echo '</div>';
}
?>
<form method="post">
<?php
echo $this->Form->input('User.password', array('type' => 'password',
'name' => '$data[User][password]',
'class' => 'form-control'));
echo $this->Form->input('User.re_password', array('type' => 'password',
'name' => 'data[User][re_password]',
'class' => 'form-control'));
?>
<input type="submit" class="button" style="float:left;margin-left:3px;" value="Save" />
<?php echo $this->Form->end();?>
</form>
请帮我,谢谢
以此替换您的ctp代码
<?php echo $this->Form->create('User'); ?>
<?php
if(isset($errors)){
echo '<div class="error">';
echo "<ul>";
foreach($errors as $error){
echo "<li><div class='error-message'>$error</div></li>";
}
echo "</ul>";
echo '</div>';
}
?>
<?php
echo $this->Form->input('User.password', array('type' => 'password',
'name' => '$data[User][password]',
'class' => 'form-control'));
echo $this->Form->input('User.re_password', array('type' => 'password',
'name' => 'data[User][re_password]',
'class' => 'form-control'));
?>
<input type="submit" class="button" style="float:left;margin-left:3px;" value="Save" />
<?php echo $this->Form->end();?>
希望这会起作用。
我刚注意到
您应该使用$this->User->save($this->request->data)
签出使用此
echo $this->Form->input('User.password', array('type' => 'password',
'class' => 'form-control'));
echo $this->Form->input('User.re_password', array('type' => 'password',
'class' => 'form-control'));
让cakephp构建的名称属性与数据库表,字段匹配。 我认为您只有1或2 ctp等,您只是从头开始构建项目,对吗?
尝试这个 :
public function beforeSave() {
if (isset($this->data['User']['password'])) {
$this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
}
return true;
}
并在功能重置
if($this->User->save($this->User->data,false))
它为我工作。 希望这对你有用!!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.