2

I am creating change password functionality, all things are working fine except the old password validation rule.here is my code

public function rules()
{
    return array(
        array('is_active', 'numerical', 'integerOnly'=>true),
        array('first_name, joining_date,last_name, employee_code, username, password, role', 'required','on'=>array('create')),     
        array('employee_code', 'numerical', 'integerOnly'=>true),
        array('username','email'),      
        array('username','valid_username','on'=>array('create')),

        //array('username', 'contraints', 'readOnly'=>true, 'on'=>'update'),

        array('currentPassword, newPassword, newPasswordRepeat', 'required','on'=>array('change')),
        //array('newPassword', 'length', 'min' => 6, 'max'=>20, 'message'=>Yii::t("translation", "{attribute} is too short.")),
        //array('newPassword','ext.SPasswordValidator.SPasswordValidator', 'preset' => 'strong', 'max' => 41),
        array('newPassword', 'compare', 'compareAttribute'=>'newPasswordRepeat','on'=>array('change')),

        array('currentPassword', 'equalPasswords','on'=>array('change')),

        array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements(),'on'=>array('forgotPassword')),
        array('joining_date', 'safe'),
        array('user_id, first_name, last_name, employee_code, username, password, role, joining_date, pending_regular_leave, pending_medical_leave, allocated_regular_leave, allocated_medical_leave, is_active', 'safe', 'on'=>'search'),
    );
}

My change password function is

public function equalPasswords($attribute, $params)
{
    $user = Users::model()->findByPk(Yii::app()->user->id); 
    if ($user->password != md5($attribute))
    {
       $this->addError($attribute, 'Old password is incorrect.');
    }   
}

Update method

:

public function actionChange()
{
        $model=new Users;
        $model->setScenario('change');

    if (isset($_POST['Users'])) {
        $model->setAttributes($_POST['Users']);                     
        if($model->validate())
            {       
                $pass = md5($_POST['Users']['newPassword']);            
                $userModel = Users::model()->findByPk(Yii::app()->user->id);
                $userModel->password = $pass; 
                $data = $userModel->update();
                Yii::app()->user->setFlash('success',"Password changed successfully!");
            }
        }

$this->render('change_password', array('model'=>$model,true));
}

when i try to change password with all the correct parameters (correct old password,new password,retype password) it update the password but also shows me error that your old password does not correct .please help me to resolve this as i am new to Yii. Thanks in advanced.

2 Answers 2

1

I'm not sure but you can try once.

$user = Users::model()->findByPk(Yii::app()->user->id); 
if ($user->password != md5($this->attributes['currentPassword']))
{
     $this->addError($attribute, 'Old password is incorrect.');
} 

change md5($attribute) to md5($this->attributes['currentPassword']

And add this in your rules

public function rules()
{
    public $currentPassword; 
    // your rules here
}
Sign up to request clarification or add additional context in comments.

Comments

0

In the validator you receive the attribute name, not the value in $attribute. To get the value you'd have to:

$value = $this->$attribute;

3 Comments

Well, then do some debugging: Add a echo $user->password; echo $this->$attribute;exit; in your equalPasswords() method. This should help you to track down the issue.
now validation are working fine when i commented the update method its works but when i uncomment the method $data = $userModel->update(); it throws me the same error i have updated my question with the update function.
Ok, but this is a different question now. Your original problem with failed validation is solved. The code you have in your controller is actually not so good. You should load the user model first, then assign the attributes there.

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.