简体   繁体   中英

Yii Model rule for validation doesn't work

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.

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

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

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'),

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM