Scenarios usage

Hi @ all !

I think I correctly coded my scenario but it would always validate all the attributes.

@the controller:


if ($model->validate('foobar_scenario')) {

//....

}

@the model:


 public function rules()

    {

        return array(

        	array('foo','required','message' => 'foo.'));

        	array('bar','required','message' => 'foo.'));

        	array('foobar','required','message' => 'foo.'));

        	array('text','required','on'=>'foobar_scenario','message' => 'foo.'));

}

Using this code i get 4 error messages, so how I can tell Yii to validate only the fields with scenario = ‘foobar_scenario’ ?

If you do not supply a scenario of a rule it will always be validated. No matter the scenario.

Here is excerpt of the documentation:

"the scenario that the validation rules should be applied. Defaults to empty string, meaning only those validation rules whose "on" option is empty will be applied. If this is a non-empty string, only the validation rules whose "on" option is empty or contains the specified scenario will be applied. As of version 1.0.4, this parameter is deprecated, and the scenario property is the preferred way of specifying the scenario. In particular, when this parameter is empty, the scenario property value will be used as the effective scenario."

Does this means I have to define empty scenarios for other fields like this :


 public function rules()

    {

        return array(

                array('foo','required','on'=>'','message' => 'foo.'));

                array('bar','required','on'=>'','message' => 'foo.'));

                array('foobar','required','on'=>'','message' => 'foo.'));

                array('text','required','on'=>'foobar_scenario','message' => 'foo.'));

}

I also tried as in the docs :




$model->scenario = 'foobar_scenario';

if ($model->validate())



or




$model->scenario = 'foobar_scenario';

if ($model->validate('foobar_scenario'))



But Yii would always validate all the fields.

Could you please give me a working sample of scenarios usage ?

What Dave says is good. If a rule has the ‘on’ option empty (or initialized in blank ‘’) then this rule will be applied in all scenarios.

You mus define diferent escenarios.

Example:




public function rules()

    {

        return array(

                array('foo','required','on'=>'otherSenario','message' => 'foo.'));

                array('bar','required','on'=>'otherSenario

','message' => 'foo.'));

                array('foobar','required','on'=>'otherSenario

','message' => 'foo.'));

                array('text','required','on'=>'otherSenario

, foobar_scenario','message' => 'foo.'));

}



Then:




$model->scenario = 'foobar_scenario';

if ($model->validate())



Does the validation of the ‘text’ property only.

And




$model->scenario = 'otherScenario';

if ($model->validate())



Does the validation of all properties

Ok I already understood that with some tests i made, thanks.

But i think there is a better scenario handling in Yii 1.1 so i’ll try to migrate :wink: