I am yet to write the last article of the Yii Session series. I just decided to write something about the scenarios in the Validation feature of Yii framework. Scenarios are a very useful tool for adding different validations rules for different cases on the same model.
In a real life situation, you would require one validation rule for user registration but the same rule might not be applicable to the User login. Scenarios help you define validation rules for different situations within same model. As an example, on registration form, you might want to validate username for uniqueness. But, on password change form, you would not want to validate the username. Instead the user password will be the required field.
In-built and Custom Scenarios
There are certain very common scenarios like insert, update, search etc. Yii framework provides an in-built support for these scenarios.
In addition, you can set any custom scenario name. A scenario is set in the rules method of the Yii model.
[php]//The rules method of a Yii Model
public function rules()
// password is required on the changepwd scenario – a Custom scenario
array(‘password’, ‘required’, ‘on’=>’changepwd’),
// list of required fields for the insert scenario
array(‘username, password, mobile, email’, ‘required’, ‘on’=>’insert’),
// No defined scenario; so applicable to all scenarios
array(‘username’, ‘length’, ‘max’=>60),
// checks username for uniqueness on the insert scenario
// The validation rule for the in-build search scenario.
array(‘username, email, mobile’, ‘safe’, ‘on’=>’search’),
Once the rules are set up, they tell which rule to be applied on which scenario. The last example sets the rule safe on the in-built search scenario. We will talk more about this in a different post.
The scenario rules are set in the model. The suitable validation rule can be applied by any of the three ways:
- By defining the scenario property of the model: This is useful for pre-existing model instances
[php]$model = User::model()->findByPk($id);
$model->scenario = ‘changepwd’; [/php]
This example is basically a switching of the scenario on an existing model instance.
- By explicitly passing the scenario to the Model constructor: When you are creating a model instance for a particular purpose, then you can directly pass the scenario name to the model constructor.
[php]$model = new User(‘changepwd’); [/php]
- By directly passing the scenario to the validate method of the model: In the above 2 cases, we are only telling the model to validate a particular scenario. The actual validation is done by the validate() method of the model. So, you can also pass the scenario directly to the validate() method.
$model1 = User::model()->findByPk($id)
$model2 = new User();
All the above examples are different ways of applying the ‘changepwd’ scenario to the User model.
Scenarios provide solution for a very common real life requirement. As we continue to explore Yii, we are finding many useful and innovative features in this framework.