Yii Framework Forum: Calling a validator from inside a custom rule - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Calling a validator from inside a custom rule Rate Topic: -----

#1 User is offline   toMeloos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-September 09

Posted 04 May 2010 - 05:50 AM

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?
0

#2 User is offline   hermes 

  • Newbie
  • Yii
  • Group: Members
  • Posts: 2
  • Joined: 26-April 10

Posted 28 May 2010 - 07:38 AM

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;
        }
    }
}

0

#3 User is offline   Mike 

  • Elite Member
  • PipPipPipPipPip
  • Yii
  • Group: Members
  • Posts: 3,013
  • Joined: 06-October 08
  • Location:Upper Palatinate

Posted 28 May 2010 - 08:26 AM

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.
2

#4 User is offline   toMeloos 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 19-September 09

Posted 23 June 2010 - 09:42 AM

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.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users