public function actionChangePassword($id) {
$model = new ChangePasswordForm('changePwd');
if (Yii::app()->user->isGuest) {
$this->redirect('login');
}
$model = ChangePasswordForm::model()->findByAttributes(array('id'=>$id));
$model->scenario = 'changePwd';
if(isset($_POST['User'])){
$model->attributes = $_POST['User'];
$valid = $model->validate();
if($valid){
$model->password = $model->new_password;
if($model->save())
$this->redirect(array('changepassword','msg'=>'successfully changed password'));
else
$this->redirect(array('changepassword','msg'=>'password not changed'));
}
}
$this->render('ChangePassword', array('model' => $model));
}
หน้า model form ครับ
Code (PHP)
class ChangePasswordForm extends CFormModel
{
public $old_password;
public $new_password;
public $repeat_password;
public function rules()
{
return array(
array('old_password, new_password, repeat_password', 'required','message'=>'กรุณากรอกรหัสผ่าน'),
array('repeat_password', 'compare', 'compareAttribute'=>'new_password','on'=>'changePwd', 'message'=>'Passwords don\'t match'),
);
}
public function findPasswords($attribute, $params)
{
$user = ChangePasswordForm::model()->findByPk(Yii::app()->user->id);
if ($user->password != $this->old_password)
$this->addError($attribute, 'Old password is incorrect.');
}
}
Code (PHP)
public function actionChangePassword($id) {
$model = new ChangePasswordForm('changePwd');
if (Yii::app()->user->isGuest) {
$this->redirect('login');
}
$model = ChangePasswordForm::model()->findByAttributes(array('id'=>$id));
$model->scenario = 'changePwd';
if(isset($_POST['User'])){
$model->attributes = $_POST['User'];
$valid = $model->validate();
if($valid){
$model->password = $model->new_password;
if($model->save())
$this->redirect(array('changepassword','msg'=>'successfully changed password'));
else
$this->redirect(array('changepassword','msg'=>'password not changed'));
}
}
$this->render('ChangePassword', array('model' => $model));
}
หน้า changepassword ครับ
มันขึ้น error ว่า your request is invalid
รบกวนผู้รู้ช่วยชี้แจงหน่อยนะครับ
ว่าจะทำยังไงให้มัน เปลี่ยนรหัสผ่านได้ครับ
ขอบคุณครับ
thank for advance