ECompositeUniqueValidator validates that the combined values of the given attributes (composite key) is unique in the corresponding database table.
It is a modification of the build in CUniqueValidator. All properties and methods of the CUniqueValidator should also work with this validator, with the exception of the property [font="Lucida Console"]attributeName[/font].
[size="5"]Requirements[/size]
Tested with Yii 1.1.9, should work with Yii 1.1 or above.
[size="5"]Usage[/size]
[size="3"]Files[/size]
Extract the zip file.
Put everything under [font="Lucida Console"]ECompositeUniqueValidator/components[/font] into your [font="Lucida Console"]protected/components[/font] folder.
Put everything under [font="Lucida Console"]ECompositeUniqueValidator/extensions[/font] into your [font="Lucida Console"]protected/extensions[/font] folder.
Make sure that the extension is imported in your config file [font="Lucida Console"]protected/config/main.php[/font]:
'import'=>array(
...
'ext.ECompositeUniqueValidator',
// 'ext.*', // <---- this works too
),
[size="3"]Example 1 (basic)[/size]
In your model file:
public function rules()
{
return array(
array('book_id, tag', 'ECompositeUniqueValidator'),
...
);
}
This rule ensures that each (book_id, tag) combination is unique in the corresponding database table.
[size="3"]Example 2[/size]
In your model file:
public function rules()
{
return array(
array('book_id, tag', 'ECompositeUniqueValidator',
'attributesToAddError'=>'tag',
'message'=>'The tag {value_tag} already exists for this book.'),
...
);
}
The property [font="Lucida Console"]attributesToAddError [/font]defines to which attribute(s) the error message should be added to, if a validation error occurrs. It should be a comma separated list of attributes. If not specified, the error will be added to the first attribute in the list of attributes.
The placeholder [font="Lucida Console"]{value_tag}[/font] will be replaced with the current value of the attribute [font="Lucida Console"]tag[/font].
[size="5"]Documentation[/size]
[size="3"]Placeholders[/size]
When using the message property to define a custom error message, the message
may contain additional placeholders that will be replaced with the actual content.
ECompositeUniqueValidator allows for the following placeholders to be specified:
-
{attributes}: replaced with a comma separated list of the labels of the given attributes.
-
{values}: replaced with a comma separated list of the current values of the given attributes.
-
{value_"attr"}: replaced with the value of the given attribute named "attr".
-
{attr_"attr"}: replaced with the label of the given attribute named "attr".
For example, use {attr_tag} and {value_tag} to get the label and the value of the attribute [font="Lucida Console"]tag[/font] (see Example 2 above).
[size="3"]Properties[/size]
ECompositeUniqueValidator introduces two additional public properties:
-
[font="Lucida Console"]attributesToAddError[/font]: comma separated list of attributes, to which the error message should be added (see Example 2 above).
-
[font="Lucida Console"]attributeNames[/font]: replaces the property [font="Lucida Console"]attributesName [/font]of the CUniqueValidator. Thus, it is a comma separated list of
the ActiveRecord class attribute names that should be used to look for the attribute values being validated.
Defaults to null, meaning using the names of the attributes being validated.
[size="3"]The Component[/size]
The abstract class CValidator
does not facilitate a combined validation of multiple attributes. Rather, each attribute is validated separately.
We circumvent this by extending the CValidator and overriding its [font="Lucida Console"]validate()[/font] function, which is where the attributes are separately sent to the validator (see class file [font="Lucida Console"]Validator.php[/font] for details).
The Validator class introduces a new property:
- [font="Lucida Console"]enableCombinedValidation[/font]: If set to true, all attributes are sent to the validator at once. Defaults to null, meaning each attribute is validated separately (default behaviour).
The ECompositeUniqueValidator is derived from Validator and has [font="Lucida Console"]enableCombinedValidation[/font] set to [font="Lucida Console"]true[/font], therefore all listed attributes are validated together.
[size="5"]Resources[/size]
-
extension page
-
composite-unique-key-validatable : An extension, that also validates composite unique keys of AR-models, implemented as a behaviour.
-
unique-attributes-validator : Another extension that validates unique constraints with more then one attribute.
That’s it. If you find bugs or have suggestions for improvements, please let me know.