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 ¶
- Initial release. Please refer to this Forum topic
Great for my purpose
Firstly great job, congrat to me23 afiouni and community.
@Inkvizitor: What's the behavior of null values ?
Good job.
One problem: extension doesn't work with NULL values correctly.
great!
Thanks for share!
Thanks
Thank you, i didn't have to reinvert the wheel. :)
Works like a charm.
@Inkvizitor: What' with NULL values?
another bug :(
Hi, what should i do to make it check the values after the beforeSave() makes changes ?
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?
Thumbs up!
Thank you!
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...)
Make it work with fields of type date
Or, you can pass the parameter when configuring the rule.
double error message
Very useful extension, but why I get two error message on validation fail?
@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?
@itmagetan
foreach ($attributes as $attribute) { if(!$this->hasError($attribute)) $this->addError($attribute,$message); }
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) }
Perfect
Does what is says on the box :)
Excellent !!
Thanks for sharing !!
Doesn't work at all
This extension doesn't work at all. On update a model, without changing anything, I get this, when I shouldn't get any error:
Please fix the following input errors:
The Combination of (Id+Language+Category) should be unique in the current context. The Combination of (Id+Language+Category) should be unique in the current context. The Combination of (Id+Language+Category) should be unique in the current context.
Moreover, I get 3 exact messages
If you have any questions, please ask in the forum instead.
Signup or Login in order to comment.