CakePHP data validation in model

This post explains about CakePHP data validation in model. We will write validations in client-side, this explains about validations in server-side in CakePHP MVC framework.

CakePHP data validation in model by Anil Kumar Panigrahi

CakePHP data validation in model by Anil Kumar Panigrahi

MySql Table:

CREATE TABLE IF NOT EXISTS `users` (
  `userId` int(11) NOT NULL AUTO_INCREMENT,
  `first_name` varchar(255) NOT NULL,
  `last_name` varchar(255) NOT NULL,
  `name` varchar(255) NOT NULL,
  `email` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `userStatus` tinyint(4) NOT NULL DEFAULT '1',
  PRIMARY KEY (`userId`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Code of CakePHP Model

Path : /CakePHP/app/model/User.php

class User extends AppModel {
public $validate = array(
        'first_name' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field is required.'
            )
        ),
        'last_name' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field is required.'
            )
        ),
        'name' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field is required.'
            )
        ),
                'email' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field is required.'
            ),
            'email' => array(
                'rule' => 'email',
                'message' => 'Please supply a valid email address.'
            ),
            'isUnique' => array(
                'rule' => 'isUnique',
                'message' => 'This email already exists in the system.'
            ),
            'systemCheck' => array(
                'rule' => 'system_check_email',
                'required'      => true,  
                'allowEmpty'    => false,  
                'message'       => 'This email already exists.'
            )
        ),
        'password' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field is required.'
            ),
            'minLength' => array(
                'rule' => array('minLength', '8'),
                'message' => 'Password must be minimum 8 characters long.'
            )
        ),
        'password_confirm' => array(
            'notEmpty' => array(
                'rule' => 'notEmpty',
                'message' => 'This field is required.'
            ),
            'match' => array(
                'rule' => 'validatePasswordConfirm',
                'required'      => true,  
                'allowEmpty'    => false,  
                'message'       => 'Passwords do not match.'
            )
        );
   function system_check_email(){
       $email = $this->data['User']['email'];
       // Write code for check the given email address is exists in DB or not
   }
   function validatePasswordConfirm($data) {
    if ($this->data['User']['password'] !== $data['password_confirm']) {  
            return false;  
        }
        return true;  
    }
}

we can write your validation methods in your model itself.

In the view:

path: /CakePHP/app/View/Users/index.php

<?php echo $this->Form->input('User.first_name', array('tabindex' => '1', 'type' => 'text', 'placeholder' => 'First Name*', 'class' => 'text required input', 'id' => 'first_name', 'label' => false, 'maxlength' => '50', 'error' => array('attributes' => array('wrap' => 'div', 'class' => 'form-error')))); if(!$this->Form->isFieldError('User.first_name')) echo '<div class="invalid"></div>'; ?>

This is sample code, we can write all fields in the view.

Anil Kumar Panigrahi

With more than 10 years experience in PHP and Founder of Anil Labs, a blog for PHP and related posts. contributed posts regarding CodeIgniter, CakePHP and Learn PHP online ... Contact : [email protected]

You may also like...

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.