Adding Criteria To Validation Rules

Sometimes you want your validation rules to be just a little bit more than the standard options available to you like ‘numeric’, ‘length’ or ‘safe’.

Occasionally you might need a value to be unique within it’s database column:

array('username', 'unique', 'className' => 'User');

But sometimes you want unique to be more intelligent with it’s unique validation…

array('username', 'unique', 'className' => 'User', 'criteria' => array(                 'condition' => 'active = 1',         ) )

The test above will only match when username is not unique AND active = 1. In the case of unique, criteria you define is appended to the existing check which would always take place.

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>