Multiple validation rules for a single field in cakephp

Cakephp framework provides a strong model validation mechanism. Here is the quick overview of multiple validation rules applied to a single field.

Let say we are using “User.password” and “User.password2” fields to accept/confirm password while registering to a site. We have “password2” field which is supposed to contain non-empty characters as well as the same password entered in the “password” field. We want to display different error message in two different conditions i.e. “Please enter a re-type password” if user did not enter the re-type password or “Re-type password does not match” when user enters a miss-matching password.

Here is the snippet of code which is supposed to be inside the view section. Note that the error messages to be displayed in case of validation failure are placed in the $form->input method so we don’t need them to be placed inside the validation rules in the model class. Placing the error messages inside the view is most logical and preferred approach.

<?php __('Password');
echo $form->input('User.password', array('label'=>false, 'error'=>__('Password must not be empty', true))); ?>
__('Re type password'); ?>
echo $form->input("User.password2", array('type'=>'password', 'label'=>false, 'error'=>array('rule1'=>__('Password and re-type do not match!', true), 'rule2'=>__('Re-type password should not be empty',true)))); ?>

Here is the example validation rules which can be placed in the User Model class to validate the password submission:

var $validate = array(
	'password'=>array(
		'rule' => array('notEmpty'),
	),
	'password2'=>array(
		'rule1' => array(
			'rule' => array('matchPasswords', 'password')
		),
		'rule2' => array(
			'rule' => array('notEmpty')
		)
	)
);

Here ‘password2’ field value is passed through multiple validation rules (two here) and set for displaying different error messages in the view. Note that the validations for multiple validation rules acts like recursive process by default, thus, the last validation failed message is set for displaying in the views.

In the above validation for “password2” field first element of array value of “rule” in “rule1” contains the name of custom function which is written within the User model class and second element in array refers to “password” field so we can use this field value inside that custom function which is passed as the second parameter by default. Here is the custom function:

function notEmptyRePassword($str1_data,$password) {
	if(!empty($this->data['User'][$password]) && empty($str1_data['password2'])) {
		return false;
	}
	if(empty($str1_data['password2'])) {
		return false;
	}
	return true;
}

In this function $str1_data contains the field value of “password2” field and $password contains the name of other field which here is “password”. Function returns true or false accordingly if no match is found.

This is a simple example of multiple validation rules for a single field. Hope this helps someone!

Thanks.

Leave a Reply