User Defined Validation in CakePHP 1.2
- January 20th, 2008
- Write comment
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.