Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • client side and sever side validation in cakephp

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 1.02k
    Comment on it

    Data validation plays an important role in any application, It helps to confirm  that the data in a Model confirms to all the rules of any application.

     

    There are 2 types of validation in cakephp. First one is server side and the next is Client side. Client side validation can be done through jquery and the server validation in cakePHP is defined in the models of cakephp.

     

    For example-->

    if you want to make sure that email entered should be in the proper format or the password entered should not be more than 8 characters or an email or username should be unique for that validation rules makes form handling much easier.

     

    The first step to data validation is creating the validation rules in the Model if we are doing server side validation. To do that, use the Model::validate array in the Model definition.

     

    Example ->

    // email, date and username validation

    class User extends AppModel {
        public $validate = array(
            'login' => 'alphaNumeric', 
            'email' => 'email',
            'born' => 'date'
        );
    }
    
    or
    
    //unique email validation
    
      public $validate = array(   
        'email' => array(
            'rule' => 'isUnique',
            'required' => true,
            'allowEmpty' => false,
            'on' => 'create', // here
            'last' => false,
            'message' => 'This email is already in use. Please try another email.'
        )

     

    For client side validation , First we should define JQUERY main file and jquery validations files for the page in which want to add validations.i.e.

    <script src="<?php  echo $this->webroot; ?>js/jquery.js"></script>
    <script src="<?php  echo $this->webroot; ?>js/jquery.validate.js"></script>

     

    after defining these two Jquery files for the page, We can add validation to each and every field like this- >>

    <script>
       
            $("#Form1").validate({
                rules: {
                    'data[DepartmentUser][fname]': { required: true,},
                    'data[DepartmentUser][lname]':{ required: true,},                
                    'data[DepartmentUser][email]': { required: true, email: true },
                 
                },
                messages: {
                    'data[DepartmentUser][fname]': {required: "Please enter First Name." },
                    'data[DepartmentUser][lname]': {required: "Please enter Last Name" },
                    'data[DepartmentUser][email]': {required: "Please enter your email address." },              
                             
                },
            });       
           
      </script> 

     

 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: