cakephp 2.3 - 更改密码方法错误

时间:2013-07-16 15:24:45

标签: php cakephp

我在控制器(UsersController.php)中使用此方法更改用户密码:

public function changepass() {
    if($this->User->exists()) {         
        $new_pass = $this->request->data['User']['newpass'];
        $repeat_pass = $this->request->data['User']['newrepeat'];
            if($new_pass == $repeat_pass) {
                $this->User->saveField('password',$new_pass);
                $this->Session->flash(__('Updated successfully'));
                $this->redirect(array('controller' => 'users','action' => 'dashboard'));
            } else {
                $this->Session->flash(__('Passwords did not match'));
                $this->redirect(array('controller' => 'users','action' => 'changepass'));
            }

    }
}

和此视图(changepass.ctp):

<?php 
    echo $this->Form->create();

    echo $this->Form->input('newpass',array('type'=>'text','label'=>array('text'=>'Enter new password'))); 

    echo $this->Form->input('newrepeat',array('type'=>'text','label'=>array('text'=>'Confirm new password'))); 
?>

    <button type="submit">Save</button>

<?php echo $this->Form->end(); ?>

但这似乎不起作用,我实际上无法理解为什么。你能帮助我吗?你觉得这里有什么不对吗?

先谢谢你们!

1 个答案:

答案 0 :(得分:2)

在检查用户是否存在之前尝试设置用户ID

假设您正在尝试更改当前登录用户的密码,并假设您的用户ID列名为user_id

$this->User->id = $this->Auth->user('user_id');

如果您可以稍微具体说明为什么它不起作用(它是否显示密码与消息不匹配等)会更容易。)