Versions Compared

Key

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

...

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