Yii 1.1: unique-multiple-columns-validate

Validates that a combination of attribute values is unique in the corresponding database table
16 followers

This extension, takes a combination of Model attributes and verifies uniqueness of the combination in the database table.

Credit: I took the original code posted by me23 at this Forum topic and packaged it with minor modification to raise an error referencing the attribute combination and not a single attribute. Other than that, the credit it to the community.

Documentation

Requirements

  • Yii 1.0 or above

Installation

  • Extract the release file under protected/extensions

Usage

See the following code example:

public function rules()
{
    return array(
        ....
        array('attribute1+attribute2+attribute3', 'application.extensions.uniqueMultiColumnValidator'),
        ....

Change Log

October 23, 2009

Total 15 comments

#16659 report it
nairgh at 2014/03/17 01:37am
Excellent !!

Thanks for sharing !!

#13774 report it
johnsnails at 2013/06/25 02:53am
Perfect

Does what is says on the box :)

#11437 report it
luk1999 at 2013/01/14 05:05am
Validating integer fields in PostgreSQL

When you use this extension with PostgreSQL and you try to validate integer type fields you get error:

function lower(integer) does not exist

Simple workaround for this: set caseSensitive to true:

public function rules() {
   array('contact_person_id+company_id', 'ext.uniqueMultiColumnValidator', 'caseSensitive' => true)
}
#10406 report it
Anatoly Rugalev at 2012/10/26 02:41am
@itmagetan
foreach ($attributes as $attribute)
{
    if(!$this->hasError($attribute))
        $this->addError($attribute,$message);
}
#9339 report it
itmagetan at 2012/08/04 07:49pm
@mohamadaliakbari : double error message

Because of looping error message. I'm not sure if it is a good way, but change the code :

foreach ($attributes as $attribute)
{
    $this->addError($object,$attribute,$message);
}

to

foreach ($attributes as $attribute)
{
    //$this->addError($object,$attribute,$message);
}
$this->addError($object,$attribute,$message);

will solve the problem. Any better approach?

#8572 report it
mohamadaliakbari at 2012/06/12 10:22am
double error message

Very useful extension, but why I get two error message on validation fail?

The Combination of (Cbn+Request) should be unique in the current context.
The Combination of (Cbn+Request) should be unique in the current context
#6865 report it
jmariani at 2012/02/09 02:52pm
Make it work with fields of type date

Or, you can pass the parameter when configuring the rule.

#6863 report it
Alichin at 2012/02/09 01:40pm
Make it work with fields of type date

Make it work with name and date combination

The extension does not work when a field is a date. This is due to the conversion to lowercase which is performed when the caseSensitive property is left to false, which is the default. My brutal fix was to change the default from false to true in row 8 of the class uniqueMultiColumnValidator.php

Hope someone will provide a better fix (testing for field type before the conv. to lowercase, etc...)

#6674 report it
jmariani at 2012/01/27 12:01pm
Thumbs up!

Thank you!

#5957 report it
Daniel at 2011/11/29 06:03pm
Excellent ext, but not working with name and date combination

Hi,

I have Member table which contains membershipNo, name and date, I want to implement a validation that combination of name and date must be unique, but the validation always through....... If I changed to membershipNo and name it works fine.

Any hints?

#2481 report it
Bibi40k at 2011/01/08 07:46am
another bug :(

Hi, what should i do to make it check the values after the beforeSave() makes changes ?

#2456 report it
szako at 2011/01/05 06:45am
Thanks

Thank you, i didn't have to reinvert the wheel. :) Works like a charm.

@Inkvizitor: What' with NULL values?

#437 report it
tydeas_dr at 2010/06/02 08:09am
Great for my purpose

Firstly great job, congrat to me23 afiouni and community. @Inkvizitor: What's the behavior of null values ?

#904 report it
Inkvizitor at 2010/02/07 06:03am
Good job.

One problem: extension doesn't work with NULL values correctly.

#1161 report it
jerry2801 at 2009/11/20 08:57am
great!

Thanks for share!

Leave a comment

Please to leave your comment.

Create extension
  • Yii Version: 1.1
  • License: Other Open Source License
  • Developed by: Khaled Afiouni
  • Category: Validation
  • Votes: +16 / -1
  • Downloaded: 3,010 times
  • Created on: Oct 23, 2009
  • Last updated: Jun 6, 2012
  • Tags: validation