composite-unique-validator

Validates that the combination of several attributes values (composite key) is unique in the given database table.
9 followers

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 attributeName.

Requirements

Tested with Yii 1.1.9, should work with Yii 1.1 or above.

Usage

Files

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 property attributesName 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 Component

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

Total 3 comments

#17887 report it
Rudra at 2014/08/06 07:24am
How can I validate for two fields of two different models

Suppose I have field A in Model A and field B in Model B. How can I validate them

#17032 report it
Sigit Purnomo at 2014/04/27 12:37am
Great

Thanks so much . :)

#16131 report it
Adbur Rob at 2014/01/22 07:47am
Very nice!

Many thanks for this extension! Works nicely.

Leave a comment

Please to leave your comment.

Create extension