hello im trying to add rule for non attribute element of a model.
i did not try to add it as a new attribute discussed as here.
anyway,
i added a checkbox in a registration form. i defined a new public property for it:
public $policy;
then i defined a new rule in rules method
...
array('policy', 'terms', 'val'=>1),
...
i saw this in docs:
class MyValidator extends CValidator
{
protected function validateAttribute($model,$attribute)
{
$value=$model->$attribute;
if($value has error)
$model->addError($attribute,$errorMessage);
}
}
then i wrote a similar code:
class terms extends CValidator
{
public $val;
protected function validateAttribute($model, $attribute)
{
$errorMessage = "You must agree with the Terms and Conditions!";
$value = $model->$attribute;
if($value !== $this->val)
$model->addError($attribute, $errorMessage);
}
}
the problem is:
since this is a non attribute element
$model->$attribute
is returning null.
i tried several things but i could not come up with something meaningful (shame on me )
but funny thing is i can see the error message now. in every submit whether checkbox checked or not