CUniqueValidator functionarity

Suppose that a 'user' belongs to a 'forum'. A 'forum' should be unique thus I can code as

  public function rules()


  {


          return array(


 (snip)


                  array('forum', 'unique'),


          );


   }


in my model class extended from CActiveRecord, and it is working fine.

On the other hand, 'user' may be unique in a certain 'forum' but not unique throughout the table, since a 'user' can belong to any 'forum'.

Then, I would like to code as

  public function rules()


  {


          return array(


 (snip)


                  array('user', 'unique', <some condition here like 'forum = xxx'>),


          );


   }


,but I found it may not have this functionarity from my investigation of the code in CUniqueValidator class as below.



         protected function validateAttribute($object,$attribute)


        {


 (snip)


                if($this->caseSensitive)


                        $exists=$object->exists("$columnName=:value",array(':value'=>$value));


                else


                        $exists=$object->exists("LOWER($columnName)=LOWER(:value)",array(':value'=>$value));


BTW, I will be able to extend CUniqueValidator for myself, but I think it is better to have this functionarty within CUniqueValidator.

Thanks in advance.

This is probably better written as a method-based validator or a customized validator class. The reason is that the query condition could vary a lot in different applications. Some need static parameters while some dynamic parameters. Writing a method-based validator would be much easier.

Thank you for your prompt answer. I am going to investigate further.

I was able to solve this issue using a method based validator (and not a bulk of code) according to your suggestion. And I would like to share this with others as a solution.

        public function rules()


        {


                return array(


 (snip)


                        array('user', 'uniqueInForum'),


                );


        }





        /**


         * Unique validator in a forum                            


         */


        public function uniqueInForum() {


          $criteria=new CDbCriteria;


          $criteria->condition ='`forumcode` ='."'".$this->forumcode."'";


          $criteria->condition .=' AND `user` ='."'".$this->user."'";





          $userCount=user::model()->count($criteria->condition);


          if ($userCount)


            $this->addError('user', 'User "'.$this->user.'" has already been taken.');


        }