0

I am codeing the functionality for the user to change the password inside the Yii framework.The view requires the user to enter his current password and his new password twice.To compare the old passwords together i added the following method in the model.

public function findPassword(){
        $user = Users::model()->findByPk(Yii::app()->user->id);
        if(password_verify($user->password,$this->oldPassword) !== true){
            $this->addError($this->attribute,'Old password is incorrect');
        }
    }

and i have the following rule inside the model.

array('old_password', 'findPassword', 'on' => 'changePwd'),

When i do this and go on the form and try to change the password i am getting the following error

1 Answer 1

1

Try this:

public function findPassword(){
    $user = Users::model()->findByPk(Yii::app()->user->id);
    if(password_verify($user->password,$this->oldPassword) !== true){
        $this->addError('old_password','Old password is incorrect');
    }
}

Example: http://www.yiiframework.com/wiki/168/create-your-own-validation-rule/

Sign up to request clarification or add additional context in comments.

2 Comments

it gives me the following error Undefined variable: attribute which makes sense since there is no $attribute inside the function.Is it good practice I used the setFlash function inside the model or should i just use those kind of errors inside of the controller.
Ok, i Edit my answer. SetFlash in controller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.