You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

Blesta uses the Input component to perform automatic error checking and error message handling. The way it works is by defining a set of rules and validating input based on those rules. The Input component is a simple, yet powerful way to validate input. As we'll see, input can even be formatted during validation, either immediately before or after the rule is executed.

Boolean Rules

Boolean rules are the simplest rules of all. Setting the rule to true will always pass validation. Similarly, setting the rule to false will always fail validation.

Boolean 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

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

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

<?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"

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

 

between

 

minLength

 

maxLength

 

betweenLength

 

PHP Rules

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

PHP 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

 

 

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.

  • No labels