Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • cakePHP model side validation

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 196
    Comment on it

    Data validation plays an important part in any of the application. It is very important to make ensure that user has input valid Fields or not. For example, passwords are at least eight characters long or more,  User Names ,emails are unique,phone number should be in valid format and all fields should not be  null it should display error message while submitting any form. Defining validation rules makes the form much easier to get invalid details, form details.

    To do data validation is to create the validation rules in the Model. For doing that we should  use the validate array in the Model to define all Fields validation

    class User extends AppModel {
        public $validate = array(  //Model::validate array
            'login' => 'alphaNumeric', // login id should be alphanumeric
            'email' => 'email', // email should be in valid format
            'DOB' => 'date' // this feild should be in date format
        );
    }
    

    If users table has login, password, email and D.O.B fields, we can add validation in model by defining these rules in model like above example.The above example shows the simple validation rules.

     

    We can add more complex validation rules in the model

     

    class User extends AppModel {
        public $validate = array(
            'login' => array(
                'alphaNumeric' => array(  //  this rule defines that login id should be alphanumeric and it will display message if invalid input occurs
                    'rule' => 'alphaNumeric', 
                    'required' => true,
                    'message' => 'Letters and numbers only'
                ),
                'between' => array(
                    'rule' => array('lengthBetween', 5, 15), // this rule defines that login id should be between 5-15 characters
                    'message' => 'Between 5 to 15 characters'
                )
            ),
            'password' => array(
                'rule' => array('minLength', '8'), //defines that password shoule be minumum 8 character in length
                'message' => 'Minimum 8 characters long'
            ),
            'email' => 'email',
            'born' => array(  // this rule defines the date format
                'rule' => 'date',
                'message' => 'Enter a valid date',
                'allowEmpty' => true
            )
        );
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: