Conditional skip for a field from model validation in a cakephp controller

In a controller, a table field could be skipped from model validations by editing the “validate” property of a model. For example, i have a “date” validation for “dob” (date of birth) field set in my “User” model. It works great everywhere except a particular place where i could accept null value for this field. In this particular case i could remove(unset) the validation for this field in my controller like this:

if(empty($this->data['User']['dob'])) {
unset($this->User->validate['dob']);
}

Hint: Please check the use of “allowEmpty”, “on” and “required” options in model’s “$validate” property and see if that fit your needs rather than using the custom unset() i just put above. I had to use it in a rare situation where i was not able to touch the “allowEmpty” and “required” options.

Leave a Reply