Composite Key Unique validator

Is it possible to add a rule to validate a composite unique key with CUniqueValidator?

UNIQUE KEY (‘email’,‘username’);

public function rules()


array(‘email’, ‘username’, ‘unique’)


Yes, of course! And why it should not be possible to do so? :)

I’m using this validator in my project, with only small difference that I validate login and e-mail for being unique among table in DB.

And how do I got it?

array(‘email’, ‘username’, ‘unique’)

gives me a validation that email is unique and username is unique doesn’t validates the composite key

Nice, could we see that?

Please, forgive me. I misunderstood you. I thought that you require much simpler solution than it turned out to be. If now I understand you correctly I would follow sugestion placed in Customizing Post Model of The Yii Blog Tutorial, where a part of rules() function is own validator named normalizeTags:

array('tags', 'normalizeTags')

followed by definition of it:

public function normalizeTags($attribute,$params)




At least I would follow this way or try to overload CUniqueValidator with own class, performing own checks of composite key.

I assume is not possible.

I read a post I cannot find now that said it will be on 1.1.

Any update on this? How can we use CUniqueValidator to check against two unique keys? For example:





I should not be allowed to add a duplicate supplier_id for the current product_id.

You cannot use CUniqueValidator to validate uniqueness of a composite key.

array(‘username, email’, ‘unique’) validates both username and email to be unique but not their composition

See my extension

Hmmm interesting, will give that a go.

Are there any plans to support composite primary keys in CUniqueValidator in a new version?