Steve
Steve

Reputation: 1211

Adding an error to the view from a function in the model

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

Upvotes: 0

Views: 27

Answers (1)

Carlos Rodriguez
Carlos Rodriguez

Reputation: 169

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/

Upvotes: 1

Related Questions