[Extension] Yii Conditional Validator

General Information ¶

Yii Conditional Validator validates any number of attributes if certain attribute validation is true.

You can use any Yii Core Validator like usually you would do or any other Class Based/Inline validator. You can even use CASCADE validation using Yii Conditional Validator itself;

Yii Conditional Validator Page

Online documentation updated.

[size="3"]Coming soon [/size]

New option to allow use dot notation on a validator attribute to get related data and use in your validation. Example:

When creating a


, assuming that a business is selected, one might need check the


(assuming business is the relation name in Customer) to validate if the customer.city is the same. It is just a hypothetic example.

Documentation and examples were improved.

This extension useful for using filters. Thanks.

Hi fantgeass, there is totally new version, more easy, available.

Thanks for let me know you liked this extension!

New Version

  • Version 0.2.0[list]
  • Usage made easier, more simplyfied and more objective;
  • New ‘dot.notation’ usage on attributes name (will be improoved on next versions);
  • Some bug fixes;


Hi, I love the concept but I’m having some trouble getting started with it. I’m trying to have three form fields (Date, Start and End) to be required when a fourth field (Ignore) is unchecked:


	 * @return array validation rules for model attributes.


	public function rules()


		// NOTE: you should only define rules for those attributes that

		// will receive user inputs.

		return array(




				'validation' => array(


					'compareValue' => false


				'dependentValidations' => array(

					array('Day, Start, End', 'required'),




			// The following rule is used by search().

			// Please remove those attributes that should not be searched.

			array('ID, CampaignID, Day, Start, End, DATEADD', 'safe', 'on'=>'search'),



The result is that I get the following error message:

Any idea what I’ve done wrong?

UPDATE: please forgive my laziness. After re-reading the examples more carefully, I have spotted my mistake (‘required’ needed to be inside another array) but I’m now having another issue:

Never seen an error quite like this before! How can an object be "incomplete"?

Hi Guys,

I had noticed one more important point when dealing with this validator. Since the dependentValidtions are done only when the conditional validation is true, the fields mentioned in dependentValidations are not considered as "safe". So we have to add one more rule to mark those fields as "safe". Actually this is a good thing and the framework is smart in handling this. Since am a newbie, just want to confirm what I found is correct or is there any other way we can normally achieve the same result?

Note: Also I am trying this in Tabular input form, so I have up to 5 rows of data for user to enter but not mandatory, user can enter none, one or two rows.

public function rules()


 return array (

	array ('customer','application.extensions.YiiConditionalValidator',




			array('required', 'message'=>'{dependentAttribute} is required if the {attribute} specified is not empty.'),







dpattayath, you are right and that is a problem that I tryed to fix during so much much time but there are no way in Yii of to do those declared attributes to be set as ‘safe’ cause Yii does it in ‘start-up’ of the app.

As workaround I use to set a safe rule apart fot desired attributes:

array(‘attr1, attr2, …’, ‘safe’, ‘on’=>‘scenarioX’),

I have made some improvements in Yii-Conditional-Validator and soon I will publish the new version.

Cool. But still I think its a valid idea. Thinking from the perspective of dependent attributes as optional validations, its good to have the user aware of declaring it as safe, to stick with the rules of the framework.

Please, check the new version (1.0.0) on GitHub:

The extension page on yiiframework.com will be updated soon.

Thanks sidtj for the great extension…It seems exactly what am looking for. I can however not seem to have it work when using it in a form capturing related data.

Here are my rules

array(‘user_id’, ‘numerical’, ‘integerOnly’=>true),

		array('event_id, campaign_id', 'length', 'max'=>7),

		array('q1, q2, q3, q4, q5, q6, q7, q8, q9, q10', 'length', 'max'=>100),			

		array('q1', 'ext.YiiConditionalValidator',

        'if' => array(

            array('event.code_id', 'compare', 'compareValue'=>"303"),


        'then' => array(

            array('q1', 'required'),



and here are the relations

public function relations()


	// NOTE: you may need to adjust the relation name and the related

	// class name for the relations automatically generated below.

	return array(

		'user' => array(self::BELONGS_TO, 'Users', 'user_id'),

		'event' => array(self::BELONGS_TO, 'EventsAll', 'event_id'),



When I try to save I get the following error though…

AdhocVoc has not a relation named "event". Check the YiiConditionalValidator rule that is using the attribute name "event.code_id".

Where am i going wrong?

I am trying to make condition with relation but Validator returns me an error:

Model has not a relation named "service". Check the YiiConditionalValidator rule that is using the attribute name "service.name"

But right under validation rules i have:

public function relations()


	return array(

		'service' => array(self::BELONGS_TO, 'SocialServices', 'service_id')



Please advice with possible solution

Please, open an issue on https://github.com/slinstj/Yii-Conditional-Validator and provide your model code and configs. As soon as I have time I will help you.