composite-unique-key-validatable Behavior that attaches methods for validation of composite unique keys of AR-models

  1. Requirements
  2. Usage
  3. Resources

To validate composite unique keys attach the ECompositeUniqueKeyValidateable behavior, declare unique keys and declare short validation method in a model class.

There are few reasons to use behavior and validation method instead of writing validation class

  • we can't attach a handler for CActiveRecord::onAfterFind() with only validator (this is needed for storing of old attributes of the model for proper validation when updating an existing record)

  • CValidator doesn't imply validation of several attributes

Requirements

Tested on Yii 1.1 and php 5.3

Usage

Attach the ECompositeUniqueKeyValidatable behavior and declare unique keys

public function behaviors() {
        return array(
            'ECompositeUniqueKeyValidatable' => array(
                'class' => 'ECompositeUniqueKeyValidatable',
                'uniqueKeys' => array(
                    'attributes' => 'login, applicationId',
                    'errorMessage' => 'Your login is already taken'
                )
            ),
        );
    }

declare simple validation method in the model class

/**
     * Validates composite unique keys
     *
     * Validates composite unique keys declared in the
     * ECompositeUniqueKeyValidatable bahavior
     */
    public function compositeUniqueKeysValidator() {
        $this->validateCompositeUniqueKeys();
    }

declare the validation rule

public function rules() {
        return array(
            // the first parameter doesn't matter, I use '*' (pretty ugly
            // definition, but I don't know a better way)
            array('*', 'compositeUniqueKeysValidator'),
        );
    }
Description of the options of unique keys
  • attributes - unique key

  • errorMessage - error message

  • errorAttributes (optional) - attributes of the model which will contain the error message

  • skipOnErrorIn (optional) - if one of this attributes contains errors then validation will be skipped

Some examples

declaring of two composite unique keys

public function behaviors() {
        return array(
            'ECompositeUniqueKeyValidatable' => array(
                'class' => 'ECompositeUniqueKeyValidatable',
                'uniqueKeys' => array(
                    array(
                        'attributes' => 'email, applicationId',
                        'errorAttributes' => 'email, email_confirmation',
                        'errorMessage' => 'This email is already registered',
                        'skipOnErrorIn' => 'email, applicationId'
                    ),
                    array(
                        'attributes' => 'login, applicationId',
                        'errorAttributes' => 'login',
                        'errorMessage' => 'Your login is already taken',
                        'skipOnErrorIn' => 'login, applicationId'
                    ),
                )
            ),
        // ...

Resources

13 0
15 followers
1 035 downloads
Yii Version: 1.1
License: BSD-2-Clause
Category: Database
Developed by: ololo
Created on: Jun 12, 2011
Last updated: 12 years ago

Downloads

show all

Related Extensions