Calling a validator from inside a custom rule

I’m trying to create a frontend that will allow administrators to define some rules on a field named ‘content’. The rules the administrator can pick from are some of Yii’s built-in ones. I want to use the built-in validators (e.g. the subclasses of CValidator).

These rules will be written to a FieldRule table. I’ve extended the Field model rules with a custom rule that refers to a model function:


    public function rules()

    {

         return array(

            array('id, user_id, field_id', 'numerical', 'integerOnly'=>true),

            array('content', 'checkRules'),

        );

    }

The function looks up in the FieldRule table which rules apply to this specific field. If there are rules, I’d like to apply them by calling the CValidator subclass. Here’s what I’ve got so far (limited to one validator type to keep the post short):


    public function checkRules($attribute,$params)

    {

            $rules = FieldRule::model()->findAll('field_id=:fieldID',array(':fieldID'=>$this->field_id));

            if ($rules)

            {

                foreach($rules as $n => $rule)

                {

                    switch ($rule->type)

                    {

                        case 'type':

                            $validator = new CTypeValidator;

                            $validator->type = array($rule->parameter);

                            if (!$validator->validate($this,array($this->content)))

                                $this->addError($attribute,yii::t('user','This is not of the correct content type.'));

                            break;

                    }

                }

            }

   }

Maybe I’m looking in the wrong direction because this isn’t working. How would I apply a CValidator to the model from a function? Is this possible at all?

it is possible to do that, but I use the stander model rule to validator my table fields,

try this :)




class Member extends CActiveRecord

{

    ......


    public function rules()

    {

         return array(

            array('type', 'checkType'),

        );

    }


    ......


    public function checkType($attribute, $params)

    {

        $memberType = $this->$attribute;

        

        if ('newbie' == $memberType)

        {

            $typeRule = array(

                array('birthday','required'),

                array('hobbies', 'required'),                

            );

        

            $validatorList = new CList;

            foreach($typeRule as $rule)

    	    {

              $typeValidator = CValidator::createValidator($rule[1],$this,$rule[0],array_slice($rule,2));

              $validatorList->add($typeValidator->validate($this));

    	    }

            return $validatorList;

        }

    }

}



The good thing is, that rules() is a function. So you can create your custom logic (e.g. query DB) to build the rules array. You should only add a private var for caching the resulting array, once it is built. This prevents that the query is executed everytime rules() is called when processing a request.

Thanks for the assistance. Temporarily moved to other parts but I’ll getting back to this part of the application shortly and will definitely try to build it like this. Hope I get it working.