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?