How to validate records before inserting in CakePHP 2.X

Author - Webner
30.07.2020
|
0 Comments
||

Problem:

Sometimes we only need to validate records before inserting operations in CakePHP. In the case of bulk insertion and CSV records validation, it’s very difficult to validate individual fields of bulk records.

Solution:

array (‘validate’ => ‘only’) parameter will do exactly that for us.

How to use:

Create validation rules in Model file
Eg.
public $validate = array (
'user_id' => array (
'numeric' => array (
'rule' => array (
'numeric'
)
)
)
);
$data = array(
'user_id' => null
);
if ($this->User->saveAll(
$data, array('validate' => 'only')
)) {
echo "validates";
} else {
echo "does not validate";
}

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *