4.1.3 Multiple Rules per Field

The technique outlined above gives us much more flexibility than simple rules assignment, but there’s an extra step we can take in order to gain more fine-grained control of data validation. The next technique we’ll outline allows us to assign multiple validation rules per model field.

If you would like to assign multiple validation rules to a single field, this is basically how it should look:

 
var $validate = array(
    'fieldName' => array(
        'ruleName' => array(
            'rule' => 'ruleName',
            // extra keys like on, required, etc. go here...
        ),
        'ruleName2' => array(
            'rule' => 'ruleName2',
            // extra keys like on, required, etc. go here...
        )
    )
);
  1. var $validate = array(
  2. 'fieldName' => array(
  3. 'ruleName' => array(
  4. 'rule' => 'ruleName',
  5. // extra keys like on, required, etc. go here...
  6. ),
  7. 'ruleName2' => array(
  8. 'rule' => 'ruleName2',
  9. // extra keys like on, required, etc. go here...
  10. )
  11. )
  12. );

As you can see, this is quite similar to what we did in the previous section. There, for each field we had only one array of validation parameters. In this case, each ‘fieldName’ consists of an array of rule indices. Each ‘ruleName’ contains a separate array of validation parameters.

This is better explained with a practical example:

var $validate = array(
    'login' => array(
        'alphanumeric' => array(
            'rule' => 'alphaNumeric',  
            'message' => 'Only alphabets and numbers allowed',
            'last' => true
         ),
        'minlength' => array(
            'rule' => array('minLength', '8'),  
            'message' => 'Minimum length of 8 characters'
        ),  
    )
);
  1. var $validate = array(
  2. 'login' => array(
  3. 'alphanumeric' => array(
  4. 'rule' => 'alphaNumeric',
  5. 'message' => 'Only alphabets and numbers allowed',
  6. 'last' => true
  7. ),
  8. 'minlength' => array(
  9. 'rule' => array('minLength', '8'),
  10. 'message' => 'Minimum length of 8 characters'
  11. ),
  12. )
  13. );

The above example defines two rules for the login field: alphanumeric and minLength. As you can see, each rule is identified with an index name. In this particular case, the index names are similar to the rules they employ, but the index name can be any name of your choosing.

By default CakePHP tries to validate a field using all the validation rules declared for it and returns the error message for the last failing rule. But if the key last is set to true for a rule and it fails, then the error message for that rule is returned and further rules are not validated. So if you prefer to show the error message for the first failing rule then set 'last' => true for each rule.

If you plan on using internationalized error messages, you will need to specify error messages in your view instead.

echo $form->input('login', array(
	'label' => __('Login', true), 
	'error' => array(
	        'alphanumeric' => __('Only alphabets and numbers allowed', true),
	        'minlength' => __('Minimum length of 8 characters', true)
        )
    )
);
  1. echo $form->input('login', array(
  2. 'label' => __('Login', true),
  3. 'error' => array(
  4. 'alphanumeric' => __('Only alphabets and numbers allowed', true),
  5. 'minlength' => __('Minimum length of 8 characters', true)
  6. )
  7. )
  8. );

The field is now fully internationalized, and you are able to remove the messages from the model. For more information on the __() function, see "Localization & Internationalization"