Yii 1.1: unique-index-validator

Validates model against table unique indexes.

This validator will extract the unique indexes from a table and will test the values against them. Each index will generate proper errors if the unique index is violated.


Created and tested with Yii 1.1.9 and MySQL. Feel free to adapt it for other database engines.


Extract the file to a folder. Example: 'protected/extensions/validators'. Add the validator to your model's rules.

Note: Since this validator will check all the model's attributes instead of a particular one, the 'attribute' parameter of the rule is not taken into account.

If the index is multicolumn, the validator will throw the error to the primaryKey attribute of the model, and will throw empty string errors to each of the attributes in order to mark them in red. It's better to use form's errorSummary to display all the errors. If the index is single column, the validator will throw the error to the attribute.

$rules = array(
        array('id', 'ext.validators.EUniqueIndexValidator'),

Total 1 comment

#17706 report it
redDevil at 2014/07/16 03:43pm
SQL Error

Can you remove the curly braces from the sql? they are throwing an error. On line 91

Leave a comment

Please to leave your comment.

Create extension