ECompositeUniqueValidator is a Yii extension that 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 attributeName
.
###Requirements Tested with Yii 1.1.9, should work with Yii 1.1 or above.
###Usage
Extract the zip file.
Put everything under ECompositeUniqueValidator/components
into your protected/components
folder.
Put everything under ECompositeUniqueValidator/extensions
into your protected/extensions
folder.
Make sure that the extension is imported in your config file protected/config/main.php
:
'import'=>array(
...
'ext.ECompositeUniqueValidator',
// 'ext.*', // <---- this works too
),
####Example 1 (Basic) 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.
####Example 2 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 attributesToAddError
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 {value_tag}
will be replaced with the current value of the attribute tag
.
###Documentation ####Placeholders 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 value of the attribute 'tag' (see Example 2 above).
####Properties ECompositeUniqueValidator introduces two additional public properties:
attributesToAddError
: comma separated list of attributes, to which the error message should be added (see Example 2 above).attributeNames
: replaces the propertyattributesName
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.
The abstract class CValidator
does not facilitate a combined validation of multiple attributes. Rather, each attribute is validated separately.
We circumvent this by introducing the new Validator component that extends CValidator and overrides its validate()
function,
which is where the attributes are separately sent to the validator (see class file Validator.php
for details).
The Validator class introduces a new property:
enableCombinedValidation
: 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 enableCombinedValidation
set to true
;
therefore all listed attributes are validated together.
###Resources
- Yii Extension Page
- Yii Forum Page
- GitHub 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.