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.
Possibly Related posts:
- Multiple validation rules for a single field in cakephp
Cakephp framework provides a strong model validation mechanism. Here is the quick overview of multiple validation rules applied to a single field. Let say we... - How to use multiple validation rule sets per model in cakephp
As default we have single var $validate rule set in the model class which is triggered on each save() method call while saving form data.... - A tip on CakePHP find list with related model conditions
You will need to add recursive=>(value) to the $params when you need to get a ‘list’ of table items using related model conditions. Referring to... - Using Inflector singularize and camelize for taking out model name in CakePHP
We can use CakePHP library’s Inflector::singularize() and Inflector::camelize() methods for taking out corresponding model name from a controller name (provided we follow CakePHP’s naming conventions... - Field type tinyint(1) would not save values other than 0 and 1 in CakePHP
Just a quick note. I had a “is_active” field in my users table which i had been using to store boolean 0 and 1 values....
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.





