Google News
logo
CakePHP - Interview Questions
What is Data Validation in CakePHP?
Data validation is an important part of any application, as it helps to make sure that the data in a Model conforms to the business rules of the application. For example, you might want to make sure that passwords are at least eight characters long, or ensure that usernames are unique. Defining validation rules makes form handling much, much easier.
 
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:
<?php
class User extends AppModel {
    var $name = 'User';
    var $validate = array();
}
?>
In the example above, the $validate array is added to the User Model, but the array contains no validation rules. Assuming that the users table has login, password, email and born fields, the example below shows some simple validation rules that apply to those fields :
<?php
class User extends AppModel {
    var $name = 'User';
    var $validate = array(
        'login' => 'alphaNumeric',
        'email' => 'email',
        'born' => 'date'
    );
}
?>
Advertisement