Versions Compared

Key

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

...

Code Block
languagephp
titleBoolean Rules
<?php
	...
	$rules = array(
		'field_1' => array(
			'empty' => array(
				'rule' => true,
				'message' => "This error will never be displayed"
			)
		),
		'field_2' => array(
			'valid' => array(
				'rule' => false,
				'message' => "This error will always be displayed" 
			)
		)
	);
	$this->Input->setRules($rules);
?>

if_set Option

It is often useful to evaluate an input field only if it is submitted (such as when an edit is being performed).  To do so, we can use the 'if_set' option.

Code Block
languagephp
titleBoolean Rules
<?php
	...
	$rules = array(
		'field_1' => array(
			'empty' => array(
				'if_set' => true,
				'rule' => "isEmpty",
				'negate' => true,
				'message' => "This rule is only evaluated if field_1 is set and not null"
			)
		)
	);
	$this->Input->setRules($rules);
?>

Built-in Rules

The Input component has a set of built in rules. The syntax for invoking these rules is a string or a single dimensional array.

...

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.

...

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]{11,}$/Di"),
				'message' => "Password2 must begin with a letter, followed by only numbers, no less than 12 characters in length."
			)
		)
	);
	...
?>

...


isDate

This built-in rule determines whether the given string is in a valid date format. It may also accept minimum and maximum date values as parameters to be used in determining whether the date to be validated is within the given range. The dates may be string-formatted dates, or UNIX timestamps.

...

Code Block
languagephp
<?php
	...
	$rules = array(
		'date_begins' => array(
			'format' => array(
				'rule' => "isDate",
				'message' => "The date_begins is not in a valid date format."
			)
		),
		'date_ends' => array(
			'format' => array(
				'rule' => array("isDate", "2013-05-01", "2013-06-01"),
				'message' => "The date_ends must be a valid date between May 1, 2013 and June 1, 2013."
			)
		)
	);
	...
?>

...


matches

This built-in rule determines whether a given string matches a custom regular expression.

...

Code Block
languagephp
<?php
	...
	$rules = array(
		'phone_number' => array(
			'format' => array(
				'rule' => array("matches", "/^([0-9]{3}\.){2}[0-9]{4}$/"),
				'message' => "The phone number must be of the format ###.###.####"
			)
		)
	);
	...
?>

...


compares

This built-in rule satisfies a logical comparison between the input and your custom comparator.

...

Code Block
languagephp
<?php
	...
	$rules = array(
		'status' => array(
			'format' => array(
				'rule' => array("compares", "==", "active"),
				'message' => "The status must be 'active'."
			)
		)
	);
	...
?>

...


between

This built-in method determines whether the input value is between a minimum and maximum value.

...

Code Block
languagephp
<?php
	...
	$rules = array(
		'score' => array(
			'format' => array(
				'rule' => array("between", "1", "100", false),
				'message' => "The score must be a number between 2 and 99."
			)
		)
	);
	...
?>

...


minLength

This built-in method determines whether the given value is at least a given character length.

...

  1. The minimum length to require

...


Code Block
languagephp
<?php
	...
	$rules = array(
		'last_name' => array(
			'format' => array(
				'rule' => array("minLength", 3),
				'message' => "Your last name must be at least 3 characters in length."
			)
		)
	);
	...
?>

 


maxLength

This built-in method determines whether the given value is at most a given character length.

...

  1. The maximum length to require

...


Code Block
languagephp
<?php
	...
	$rules = array(
		'last_name' => array(
			'format' => array(
				'rule' => array("maxLength", 32),
				'message' => "Your last name must not exceed 32 characters in length."
			)
		)
	);
	...
?>

...


betweenLength

This built-in method determines whether the given value is between both the minimum and maximum character lengths.

...

  1. The minimum length to require
  2. The maximum length to require

...


Code Block
languagephp
<?php
	...
	$rules = array(
		'last_name' => array(
			'format' => array(
				'rule' => array("betweenLength", 3, 32),
				'message' => "Your last name must be between 3 and 32 characters in length."
			)
		)
	);
	...
?>

 


PHP Rules

In additional to the built-in Input rules, you can also invoke any PHP function as a rule as well.

...

Code Block
languagephp
<?php
	...	
	$rules = array(
		'status' => array(
			'format' => array(
				'rule' => array("in_array", array("active", "pending", "in_review")),
				'message' => "The given status is invalid. Valid statuses are 'active', 'pending', or 'in_review'."
			)
		)
	);
	...
?>

...


Custom Rules

You may also define your own custom rules using a callback function to perform input validation. All callback validation methods must return boolean true if the input value validates successfully and boolean false if the validation fails. They must also be public methods. The syntax for the rule is a two-dimensional array containing the reference to the class the validation method resides, and to the method itself, followed by any additional parameters for that method.

...

Code Block
languagephp
<?php
class MyClass {
	...
	private function getRules() {
		return array(
			'invoice_id' => array(
				'exists' => array(
					'rule' => array(array($this, "validateExists"), 256),
					'message' => "The invoice ID does not exist."
				)
			)
		);
	}

	public function validateExists($invoice_id, $client_id=null) {
		Loader::loadComponents($this, array("Record"));

		// Query the database to check whether the invoice ID exists
		$this->Record->select()->from("invoices")->where("invoices.id", "=", $invoice_id);
	
		// If a client ID is given, require the invoice belong to that client
		if ($client_id)
			$this->Record->where("invoices.client_id", "=", $client_id);

		return ($this->Record->numResults() > 0);
	}
	...
}
?>

 


Formatting

There are two attributes that handle data formatting during validation. They are:

...

Code Block
languagephp
<?php
	...	
	$rules = array(
		'number' => array(
			'format' => array(
				'pre_format' => array(array($this, "formatNumber")),
				'rule' => array("minLength", 4),
				'message' => "Please enter a number at least 4 digits in length."
			)
		)
	);
	...

	public function formatNumber($number) {
		return preg_replace("/[^0-9]*/", "", $number);
	}
?>

...


The following example validates that an input name is given, and then performs PHP's trim function on the input to remove white-space before and after the string:

...