Help to understanding about Scenario...

Hi sir.

Im dont understand about scenario. what is it. for a while I know the scenario is using in model validation such as:


    

public function rules(){

        return array(

            array('username,email,password1,password2,display_name,status','required', 'on' => 'create'),

            array('email','email'),

            array('username','length','max' => 15),

            array('password1','compare','compareAttribute' => 'password2')

        );

    }



the code above using "register" scenario.

I am confused what it means on the register scenario?

then what else besides registers scenario that could be used?

please help me, I’m very confused as to create a validation for a password when the password is filled, but when not filled the password will ignored.

thanks

this


'on' => 'create'

means on create scenario not on register. This


'on' => 'register'

would mean on register scenario which you don’t have in your code.

So with your current code in order to validate the required validator for your password you should define in your action $model->scenario=‘register’…than validate.

If this does not help please paste some of your action code where you handle your password…etc. The rest of your validators work because you have not setup any scenario…so they are validated in all cases.

Cheers,

bettor

hi sir. thanks for reply. here is some of my action controller


public function actionUpdate(){

        $id = (int) $_GET['id'];

        $Criteria = new CDbCriteria();

        $Criteria->condition = "id = '$id'";

        $model = SuperAdmins::loadModel($Criteria);

        $this->render('update', array(

                'model' => $model,

                'action' => array('admin/update_process')

        ));

    }


    public function actionUpdate_Process(){

        $id = (int) $_POST['id'];

        $Criteria = new CDbCriteria();

        $Criteria->condition = "id = '$id'";

        $model = SuperAdmins::loadModel($Criteria);

        $model->attributes = $_POST['SuperAdmins'];

        $model->password1  = $_POST['password1'];

        $model->password2  = $_POST['password2'];

        $model->password   = $model->password1;


        $updated = array();

        if(!empty($model->password)){

            $updated[] = 'password';

        }

        $updated[] = 'username';

        $updated[] = 'email';

        $updated[] = 'display_name';

        $updated[] = 'status';

        $updated[] = 'edited_time';

        $updated[] = 'edited_by';


        if($model->validate($updated)){

            if($model->update($updated)){

                Yii::app()->user->setFlash('message','Data Saved');

                $this->redirect(array('admin/list'));

            }else{

                Yii::app()->user->setFlash('message',CHtml::errorSummary($model));

                $this->redirect(array('admin/update','id' => $id));

            }

        }else{

            Yii::app()->user->setFlash('message',CHtml::errorSummary($model));

            $this->redirect(array('admin/update','id' => $id));

        }

    }

I want to update some of data (admin user) including the password. but if the password field is not filled it will be ignored (optional).

and here is my model’s rule


    public function attributeLabels(){

        return array(

            'username'  => 'Username',

            'email'     => 'Email',

            'password1'  => 'Password',

            'password2' => 'Confirm Password',

            'display_name' => 'Display Name',

            'status'    => 'Status'

        );

    }


    public function rules(){

        return array(

            array('username,email,password1,password2,display_name,status','required'),

            array('email','email'),

            array('username','length','max' => 15),

            array('password1','compare','compareAttribute' => 'password2')

        );

    }

thank sir. I hope this script is enough and easy to understanding about my problems that I get now

thanks

hm your code is messy :P what are you trying to achieve with if($model->validate($updated)){

This looks like you are trying to validate a scenario which you don’t have in your model. Just go if($model->validate()

are you trying to setup senario based validation or not?