User Defined Validation in CakePHP 1.2
I am really enjoying the flexibility that CakePHP 1.2 gives when it comes to validating forms. While there is a decent list of validation rules right out of the box, creating your own is pretty easy using the userDefined method. Thanks to Daniel and his article at www.4webby.com/blog for turning me on to this method. Since I like and learn from code examples, I’m going to use validating a name field as an example in this article. Here are the rules I would like to achieve:
- Alphanumeric characters are allowed. Numbers are allowed for names like Fred Jones the 2nd.
- White space must be allowed.
- The period character must be allowed.
- The name field cannot be empty
- The name field must be a minimum of 3 characters or more.
That said I would also like different messages printed for each in the following order:
- For leaving a field empty.
- For not entering 3 or more characters in the field.
- For entering non alphanumeric, white space, or the period character.
Setting It Up
Setting up multiple rules in the validation attribute is done like this:
public $validate = array(
'name' => array(
'alphaNumericws'=>array(
'rule'=>array('userDefined', 'MyModel', 'wAlphaNumeric'),
'message'=>'Only alphabets, numbers, white space, and the period characters are allowed.'
),
'minlength'=>array(
'rule'=>array('minLength', '3'),
'message'=>'Minimum length of 3 characters.'
),
'notempty'=>array(
'rule'=>array('userDefined', 'MyModel', 'notEmpty'),
'message'=>'Please enter in your name.'
)
)
The syntax for using userDefined goes like this:
'rule'=>array('userDefined', 'model_class_where_defined_method_is_located', 'methodName')
In the example I’ve added 2 of my own defined validation methods: wAlphaNumeric and notEmpty. While CakPHP already has an alphaNumeric method it doesn’t let you white list any characters. In this case I wanted to white list ‘white space’ and the period character to knock out rules 1, 2, and 3 from the list above. Here is the corresponding method:
/** check alpha numeric but white space is allowed */
public function wAlphaNumeric($check, $params) {
$valid = false;
$pattern = "^([a-zA-Z0-9\ \.]+)$";
if(ereg($pattern, $check, $test)) {
$valid = true;
}
return $valid;
}
My second user defined rule is nothing more than a replacement for VALID_NOT_EMPTY in CakePHP 1.1. I read that this has been marked to be discontinued and did not want to use it. Here is the method:
/** VALID_NOT_EMPTY replacement */
public function notEmpty($check, $params) {
$valid = false;
if(!empty($check)) {
$valid = true;
}
return $valid;
}
Here is the full code example:
public $name = 'MyModel';
public $validate = array(
'name' => array(
'alphaNumericws'=>array(
'rule'=>array('userDefined', 'MyModel', 'wAlphaNumeric'),
'message'=>'Only alphabets, numbers, white space, and the period characters are allowed.'
),
'minlength'=>array(
'rule'=>array('minLength', '3'),
'message'=>'Minimum length of 3 characters.'
),
'notempty'=>array(
'rule'=>array('userDefined', 'MyModel', 'notEmpty'),
'message'=>'Please enter in your name.'
)
)
);
/** check alpha numeric but white space is allowed */
public function wAlphaNumeric($check, $params) {
$valid = false;
$pattern = "^([a-zA-Z0-9\ \.]+)$";
if(ereg($pattern, $check, $test)) {
$valid = true;
}
return $valid;
}
/** VALID_NOT_EMPTY replacement */
public function notEmpty($check, $params) {
$valid = false;
if(!empty($check)) {
$valid = true;
}
return $valid;
}
Using the userDefined() method is extremely flexible. Another cool thing about doing it this way is that I can share the validation methods in other models.





March 12th, 2008 at 4:04 am
Example is good.But i need more detail about user defined Validation.Plz email me on my Id {gursewak_scjp@rediff.com}
August 10th, 2008 at 4:16 am
Hi – I’m not too sure if Cake 1.2 RC2 changed this bit of functionality, but I found the userDefined part didn’t work for me. The rule Key in the validation array seemed to need the name to a valid callback function, and moaned about the value being an array. What I did to solve this problem, was to put my user-defined validation function on the app_model and then called it as such:
var $validate = array(
‘sOrganisationBranchName’
=> array(
‘alphanumericws’ => array(
‘rule’ => ‘valAlphaNumeric’,
‘required’ => true,
‘message’ => ‘Alpha-numeric with spaces only.’
)
)
);
The callback function on the app_model:
public function valAlphaNumeric($arrCheck, $arrParams) {
$arrStringValues = array_values($arrCheck);
$sStringCheck = $arrStringValues[0];
$bValid = false;
$sPattern = “^([a-zA-Z0-9\ \. - ]+)$”;
if(ereg($sPattern, $sStringCheck, $test)) {
$bValid = true;
}
return $bValid;
}