This validator class validates that for the given $object, only a single record with $attribute_name = $single_bool_value exists in the DB. Where is it useful? This is best answered in an example: suppose you have polls system and only one 'poll' can be 'published on frontpage'. This property (published on front page) is represented in the DB in the form of some boolean column. Using this validator you can make sure that only one such poll is always "promoted to front page". This validator will not alter the existing poll's 'promoted' field - that's another business logic not handled here.
Algorithmicly speaking, this means that:
Tested on Yii v1.1.10. Should probably work on older versions but didn't try.
return array( // other validation rules appear here... array(attribute_name, 'UniqueBoolValueValidator'), // other validation rules appear here... );
The default value allowed only once in the DB is true. If you wish it to be false, do as follows:
return array( // other validation rules appear here... array(attribute_name, 'UniqueBoolValueValidator', 'single_bool_value' => false), // other validation rules appear here... );
July 1st, 2012: v1.0 - updated 'category' when calling Yii::t() to something more general. Please update your usage according to your preferences.
Be the first person to leave a comment
Please login to leave your comment.