Yii Framework – Scenarios for Conditional Multiple Validation Rules

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.

//The rules method of a Yii Model

public function rules()


return array(

// 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

array('username', 'unique','on'=>'insert'),

// 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.

Validating Scenarios

The scenario rules are set in the model. The suitable validation rule can be applied by any of the three ways:

  1. By defining the scenario property of the model: This is useful for pre-existing model instances
$model = User::model()->findByPk($id);


$model->scenario = 'changepwd'; 

This example is basically a switching of the scenario on an existing model instance.

  1. 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.
$model = new User('changepwd'); 
  1. 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.
//Case 1

$model1 = User::model()->findByPk($id)

//Case 2

$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.

One Response to “Yii Framework – Scenarios for Conditional Multiple Validation Rules”

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>