0
votes

I am new to Yii. I have created password changing form, page name is change. In that page there are three fields old password, new password and confirm new password fields.

My rule

    public function rules() {
    return array(
        array('old_password, confirm_password, str_user_password', 'required',
            'on' => 'change'),
        array('confirm_password', 'compare', 'compareAttribute' => 'str_user_password',
            'message' => 'Password Must Be same.', 'on' => 'change'),
    );
}

Controller

public function actionChange($id)
    {
        $model=new User('change');
        //$model->setScenario('change');
        $model=$this->loadModel($id);
        if(isset($_POST['User']))
            {
                $model->attributes=$_POST['User'];
                if($model->save())
                $this->redirect(array('view','id'=>$model->int_user_id));

            }

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

create and update page validation work. I have copied it into update page it is working perfectly. I think it's scenario problem. Please help me to find a solution.

2
I think problem in your loadModel position put on top than set scenario. loadmodel create new model and your model scenario is override by this - naveen goyal
@naveengoyal nope.. i do not think it will solve the problem. - Let me see
can you post the form and the whole model class? - Let me see
@Letmesee just look load model code.its use findbypk method. which create new model - naveen goyal
@naveengoyal yes you are right. post this as answer bro. it is the solution - Let me see

2 Answers

0
votes

You must compare new password with confirm password fields:

array('confirm_password', 'compare', 'compareAttribute' => 'new_password',
        'message' => 'Password Must Be same.', 'on' => 'change'),

or

array('new_password', 'compare', 'compareAttribute' => 'confirm_password',
        'message' => 'Password Must Be same.', 'on' => 'change'),
1
votes

Try this... I think Load model create new model that override your scenario.

    $model=$this->loadModel($id);
    $model->setScenario('change');