Yii Framework Forum: Help to understanding about Scenario... - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Help to understanding about Scenario... Rate Topic: -----

#1 User is offline   Dan Din Dun 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 09-February 10

Posted 01 March 2010 - 04:09 AM

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
Thanks and Kind Regards
------
DanDinDun
0

#2 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 01 March 2010 - 04:23 AM

View PostDan Din Dun, on 01 March 2010 - 04:09 AM, said:

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
0

#3 User is offline   Dan Din Dun 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 77
  • Joined: 09-February 10

Posted 01 March 2010 - 04:30 AM

View Postbettor, on 01 March 2010 - 04:23 AM, said:

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
Thanks and Kind Regards
------
DanDinDun
0

#4 User is offline   bettor 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 752
  • Joined: 02-February 09

Posted 01 March 2010 - 06:56 AM

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?
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users