Limited uniqueness

I remember that this idea was once posted on this forum, unfortunately I cannot dig it now. At that time, Qiang replied this is so application specific that the framework does not take care of it.

Since this is the second time for me to come across this situation, I would like to post this again.

  • A User has MailingLists (A MailingList belongs to a User)

  • A MailingList has Subscribers (A Subscriber belongs to a MailingList)

In this case, we can apply the ‘unique’ rule to the mailinglist->name since this should be unique in the system wide. On the other hand, the subscriber->name is not necessary be unique in the system wide, but should be unique in a mailinglist.

More detailed situation, on the ‘insert’ scenario, subscriber’s name must be unique in a mailinglist, i.e. no other same name should exists in a mailinglist. On the other hand, on the ‘update’ scenario, the name should be unique except itself, i.e. the record having same name and same id should exists but no other record having same name and different id should exists.

I would like to have this rule as this is not so application specific. If there is a relation, it almost always have this kind of limited uniqueness.

In my opinion, it doesn’t need much effort to make use of CActiveRecord::exists in extended beforeValidate.

I suggest you this because implementing such a validator would make validation rules much less readable.

Thanks. I do not insist on this, but I have to do this frequently, that is because I wanted it to be a standard validator.