Up to now the validation support consisted of four constants (VALID_NOT_EMPTY, VALID_NUMBER, VALID_EMAIL, and VALID_YEAR), everything else you had to do yourself. The new validation class in CakePHP 1.2 is a bit more powerful ;-)

The aforementioned constants still work in 1.2, but they are deprecated, so for new code you shouldn’t use them. 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…

Thanks to Nate for his initial explanations in the Google Groups.

Update 2007-01-06: minLength and maxLength have been implemented in changeset 4264. They can be used to validate the length of a string. If a string must consist of at least three characters you would do:

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)));

Update 2007-01-07: Also ip has been implemented, which is used to validate IP addresses.

var $validate = array('ip' => array('rule' => 'ip'));