After checking different blogs and tutorials, the bakery, API and IRC channel, it was obvious that some kind of documentation for the validation methods available in the Model was necessary. I can’t say that I will fulfill this mission but I’ll at least share what I came up with for future reference.
Here is the ‘User’ model I will be using in my example:
class User extends AppModel
{
var $name = 'User'; //optional
var $validate = array(
‘username’ => array(
array(
‘allowEmpty’ => false,
‘required’ => true,
‘rule’ => ‘alphaNumeric’,
‘message’ => ‘Username should only contain alpha-numeric characters.’,
),
array(
‘rule’ => array(’between’, 3, 10),
‘message’ => ‘User should be between 3 and 10 characters long.’,
),
array(
‘rule’ => ‘isUnique’,
‘message’ => ‘Username is already in use.’,
),
),
‘passwd’ => array(
‘alphaNumeric’ => array(
‘allowEmpty’ => false,
‘required’ => true,
‘rule’ => ‘alphaNumeric’,
‘message’ => ‘Username should only contain alpha-numeric characters.’,
),
‘validLength’ => array(
‘rule’ => array(’between’, 3, 10),
‘message’ => ‘User should be between 3 and 10 characters long.’,
),
),
‘website’ => array(
array(
‘rule’ => ‘url’,
‘on’ => ‘update’,
‘message’ => ‘Invalid URL.’,
),
),
‘agree_tos’ => array(
array(
‘allowEmpty’ => false,
‘required’ => true,
‘on’ => ‘create’,
),
),
);
);
}
That’s a lot of validation rules, I know - I just wanted to try covering the multiple ways of using the Model->validates() method.
Read the rest of this entry »