This i s basic Approch that the data we need to enter in database is correct and Validating data is must for it i am providing few of examples to see the validation methods via Models
In next session i will tell u how to be validate data via controller and vai java script validation
but Now focus on Validation via Models
. The usage of the new validation rules follows the following pattern (in your model):
var $validate = array('field' => array('rule' =>array('validationmethod','param1', 'param2')));
Ok, let’s have a look at the validation methods.
alphaNumeric should be self-explanatory, it allows only letters and strings. Useful for e.g. user names:
var $validate = array('username' => array('rule' => array('alphaNumeric')));
between is a bit misleading, as I expected that it checks whether a number is in a certain range. But what it actually does is to check the string length. In the following example the length of the user name must be at least 3 characters, and maximal 10 characters:
var $validate = array('username' => array('rule' =>array('between', 3, 10)));
blank validates that a field is blank or contains only whitespace characters like tabs or spaces. It doesn’t look very useful to me. Its usage is:
var $validate = array('x' => array('rule' => array('blank')));
cc doesn’t stand for “carbon copy”, it stands for “credit card”. It checks whether a credit card number is valid. If you want to check for Visa numbers, you would do the following:
var $validate = array('creditcard' => array('rule' => array('cc', array('visa'))));
comparison is used to compare numeric values. It supports “is greater”, “is less”, “greater or equal”, “less or equal”, “is less”, “equal to”, and “not equal”. To check whether the provided age was higher than 18 you would do:
var $validate = array('age' => array('rule' => array('comparison', '>=', 18)));
or
var $validate = array('age' =>array('rule' => array('comparison', 'greater or equal', 18)));
custom is also a bit misleading imho. It allows you to define a regular expression.
var $validate = array('username' => array('rule' =>array('custom', '/^[0-9a-z]+$/i')));
date checks for a valid date. It allows different formats, see the
API for details. To check whether the entered date is of the format yyyy-mm-dd resp. yy-mm-dd you would do:
var $validate = array('startdate' => array('rule' => array('date', 'ymd')));
decimal checks for a valid decimal number. If you want to have decimal numbers with two digits after the dot you have to use:
var $validate = array('price' =>array('rule' => array('decimal', 2)));
It is obvious, what
email does: it checks for a valid email address:
var $validate = array('email' => array('rule' => array('email')));
I am not sure whether
money is fully implemented yet, at least I was not able to use this validation rule.
numeric is a simple wrapper for the PHP function
is_numeric:
var $validate = array('age' => array('rule' => array('numeric')));
phone validates phone numbers. At the moment it supports only US phone numbers, if you want to validate other phone numbers you have to provide a regular expression.
var $validate = array('phone' => array('rule' => array('phone', null, 'us')));
postal is used to validate zip codes from the U.S., Canada, and the U.K. For other zip codes you have to provide a regular expression.
var $validate = array('zipcode' =>array('rule' => array('postal', null, 'us')));
ssn validates social security numbers from the U.S., Denmark, and the Netherlands. For other social security numbers you have to provide a regular expression.
var $validate = array('ssn' => array('rule' => array('ssn', null, 'us')));
url is used to validate urls.
var $validate = array('website' => array('rule' => array('url')));
If you look at the validation class you will notice several other public methods.
equalTo,
file,
ip,
minLength,
maxLength,
multiple, and
number are validation rules which are not implemented yet, so I don’t have described them. The
userDefined method looks like it allows you to create custom validation methods, but up to now I couldn’t figure out how to use it…
var $validate = array('username' =>array('rule' => array('minLength', 3)));
And to validate that a string can consist of maximal 20 characters you do:
var $validate = array('username' =>array('rule' => array('maxLength', 20)));
Also
ip has been implemented, which is used to validate IP addresses.
var $validate = array('ip' => array('rule' => 'ip'));
More validation methods are
- between
- blank
- boolean
- cc
- comparison
- date
- decimal
- email
- equalTo
- extension
- file
- ip
- isUnique
- minLength
- maxLength
- money
- multiple
- inList
- numeric
- notEmpty
- phone
- postal
- range
- ssn
- url
Examples
<?php
class User extends AppModel
{
var $name = 'User';
var $validate = array(
'login' => '/[a-z0-9\_\-]{3,}$/i',
'password' => VALID_NOT_EMPTY,
'email' => VALID_EMAIL,
'born' => VALID_NUMBER
);
}
?>