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:

1
2
3
4
5
6
7
8
9
10
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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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

1
<?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.


0 Comments

Leave a Reply

Avatar placeholder

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