exist validator: reference to another field of the model


I have a CFormModel M, which has fields a and b.

a is a unique key in table S.

aId is the primary key in table S

(aId, B ) is a unique key in table T.

In a form, I’d like to check that the field “b” exists in the table “T” where the field “aId” corresponds to the “Aid” of the record which has the value given in the form.

So, in the rules, I’d like to reference the value of another form attribute!

I’m trying to do this with the “exist” validator.

    public function rules()


            return array(

                    array('a, b', 'required'),

                    array('a', 'exist', 'className'=>'S', 'attributeName'=>'a'),

                    array('b', 'exist', 'className'=>'T', 'attributeName'=>'b',

                          'criteria'=>array('condition'=>'aId = (select aId from S where a = \'$object->code\')')),

                    array('verifyCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),



Of course, this doesn’t work… Looking at the code of the validator, I don’t think it’s possible. Does anyone have a workaround or a best-practice to do what I want to do?



If is not working the validator, you can always write a custom validation function.

Take a look at this comment.