Versions Compared

Key

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

...

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

...

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 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:

...