I got an interesting behavior in my CFormModel. I have a form with two input text fields. Let’s say firstname and lastname. The lastname field is required. The firstname is not required but I want to do some custom validation if it is not empty. My rules array looks as follows:
public function rules()
{
return array(
array('lastname', 'required', 'message' =>'Some error message...'),
array('firstname', 'customValidation'),
array('firstname', 'safe'),
);
}
My customValidation looks like:
public function customValidation($attribute,$params)
{
$this->addError($attribute,'error message');
}
Now, if I submit the form the customValidation will never trigger.
It only works if I remove the required validator!
This seems very strange to me and I am wondering what I am doing wrong here…
I removed the ‘safe’ declaration but the custom validation will still not work if the ‘required’ validator is active.
Please let me know what you think.