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);
?>

Built-in 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.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
titleBuilt-in Boolean Rules
<?php
	...
	$rules = array(
		'field_1' => array(
			'empty' => array(
				'if_set' => true,
				'rule' => "isEmpty",
				'negate' => true,
				'message' => "Field 1 mayThis rule is only evaluated if field_1 is set and not be empty.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
titleBuilt-in Rules
<?php
	...
	$rules = array(
		'field_1' => array(
			'empty	'field_2' => array(
			'valid' => array(
				'rule' => array("maxLength",32),
				'messagerule' => "Field 2 must be 32 characters or less." "isEmpty",
				'negate' => true,
				'message' => "Field 1 may not be empty."
			)
		),
	);
	$this->Input->setRules($rules);
?>

isEmpty

		'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 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.

...

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.

Note
titlePHP language constructs are not functions

It's important to note that PHP has a number of language constructs that look like functions but are not. You can not use language constructs as rules.

Custom Rules

In addition to the basic built-in rules that are already supported, you may 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. 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.

Consider the example below that validates whether an input value is in a predefined array using PHP's "in_array" function:

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.

Consider the example below which validates whether a invoice record exists in the database, assuming that the validation method is apart of the same class where the validation takes place.

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:

  • pre_format
  • post_format

As their names suggest they allow formatting of data before, or after, validation. These callbacks work just like the callbacks we've seen for rule validation.

The following example pre-formats the given input number into only digits and validates that it is at least 4 digits in length:Consider the example below which validates whether a invoice record exists in the database, assuming that the validation method is apart of the same class where the validation takes place.

Code Block
languagephp
<?php
class MyClass {
	...	
	private$rules function= getRulesarray() {
		return'number' => array(
			'invoice_idformat' => array(
				'existspre_format' => array(array($this, "formatNumber")),
					'rule' => array(array($this, "validateExistsminLength"), 2564),
					'message' => "The invoice ID does not existPlease enter a number at least 4 digits in length."
				)
			)
		);
	}...

	public function validateExists($invoice_id, $client_id=nullformatNumber($number) {
		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:

  • pre_format
  • post_format

...

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:

Code Block
languagephp
<?php
	...	
	$rules = array(
		'name' => array(
			'format' => array(
				'rule' => "isEmpty",
				'negate' => true,
				'message' => "Please enter your name",
				'post_format' => array("trim")
			)
		)
	);
	...
?>