我在控制器(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(); ?>
但这似乎不起作用,我真的不明白为什么。你能帮我吗?你看到这里出了什么问题吗?
提前谢谢各位!
在检查用户是否存在之前,请尝试设置用户id
假设您正在尝试更改当前登录用户的密码,并且假设您的用户id列命名为user_id
$this->User->id = $this->Auth->user('user_id');
如果你能稍微具体一点它不起作用的原因(它是否显示密码与消息不匹配等),会更容易