top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to validate data in CakePhp?

0 votes
409 views
How to validate data in CakePhp?
posted Jul 30, 2014 by Rahul Mahajan

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

The first step to data validation is creating the validation rules in the Model. To do that, use the Model::validate array in the Model definition, for example:

class User extends AppModel {
public $validate = array(
'login' => array(
'alphaNumeric' => array(
'rule' => 'alphaNumeric',
'required' => true,
'message' => 'Letters and numbers only'
),
'between' => array(
'rule' => array('between', 5, 15),
'message' => 'Between 5 to 15 characters'
)
),
'password' => array(
'rule' => array('minLength', '8'),
'message' => 'Minimum 8 characters long'
),
'email' => 'email',
'born' => array(
'rule' => 'date',
'message' => 'Enter a valid date',
'allowEmpty' => true
)
);
}

Here the different rules have been defined for different fields and these fields will be checked for validation at the time of calling of save() function in your controller. Only After validating these, the data will get saved into the database.

answer Jul 31, 2014 by Amritpal Singh
...