Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
languagephp
titleBuilt-in Rules
<?php
	...
	$rules = array(
		'field_1' => array(
			'empty' => array(
				'rule' => "isEmpty",
				'negate' => true,
				'message' => "Field 1 may not be empty."
			)
		),
		'field_2' => array(
			'valid' => array(
				'rule' => array("maxLength",32),
				'message' => "Field 2 must be 32 characters or less." 
			)
		)
	);
	$this->Input->setRules($rules);
?>

isEmpty

This built-in rule determines whether or not content passed to it is empty. This may be boolean false, an integer 0, an empty string (i.e. ""), null, etc. It is common to set the "negate" field with this rule, as often the validation rule should only accept data that is not empty, rather than what is empty. The negate field simply tells the rule to validate against the negation of the rule's return value.

Code Block
languagephp
<?php
	...
	$rules = array(
		'field_1' => array(
			'empty' => array(
				'rule' => "isEmpty",
				'negate' => true,
				'message' => "Field 1 may not be empty."
			)
		)
	);
	...
?>

 

isPassword

This built-in rule determines whether a given string matches a regular expression for validating passwords. It may accept additional parameters identifying the minimum length of the password; the type of regular expression to validate against, in the case you choose to use an existing expression already supported; or your own custom regular expression.

This rule accepts multiple parameters:

  1. The minimum length required for the password
  2. The type of regular expression to use, which must be one of the following:
    1. any - Validates anything that meets the minimum length requirement
    2. any_no_space - Validates anything that meets the minimum length requirement and does not contain spaces
    3. alpha_num - Validates anything that meets the minimum length requirement and is alpha-numeric
    4. alpha - Validates anything that meets the minimum length requirement and contains only alpha characters
    5. num - Validates anything that meets the minimum length requirement and contains only integers
    6. custom - Specifies that you want to set your own custom regular expression as the next parameter
  3. The custom regular expression you would like to use, if the type of regular expression is set to "custom"

Code Block
languagephp
<?php
	...
	$rules = array(
		'password1' => array(
			'format' => array(
				'rule' => array("isPassword", 8, "alpha_num"),
				'message' => "Password1 must be at least 8 characters in length, and alpha-numeric."
			)
		),
		'password2' => array(
			'format' => array(
				'rule' => array("isPassword", 0, "custom", "/^([a-z][0-9]+){12,}$/Di"),
				'message' => "Password2 must begin with a letter, followed by only numbers, no less than 12 characters in length."
			)
		)
	);
	...
?>

 

isDate

 

matches

 

compares

 

...