Validator

List of Validators

The Zoop Validator Component is used by a number of the other components, particularly guiControl and Forms.

It provides type based validation that can be used to validate user_input, or anything else you can think of.

Here is a table of the current validators:

Type PHP JS Notes
Phone x x US phone only.
Length x x Accepts min and max params.
Quantity x For quantity in an array (like boxes checked). Accepts min and max params.
EqualTo x Requires equal_id param (id of another field).
Int x x Accepts min and max params.
Float x x Accepts min and max params.
Numeric x x Accepts min and max params.
AlphaNumeric x x
Money x x
Zip x x Canadian or US formatted Zip (postal) code.
Email x x
Domain x doesn't start with http:// or https://
Url x x starts with http:// or https://
IP x
Password x Validates for a secure password... 1 UC, 2 LC and 1 Num and Length > 6
SSN x
RegExp x Requires Parameter RegExp Which needs to be a valid preg style Expression
DbUnique x Requires Parameters table and field. It will check the table in the default sql connection to see if a duplicate value exists in that table.
Date x Accepts Parameter format. Accepted values for format are "writeout", "us", "european", "univeral".

Using Validate

All the validation functions are statically callable. They follow the following pattern, using Int as an example.

The Call:

Validator::validateInt($value, array('min' => 3, 'max' => 7));

Returns an array ($result), with the following structure:

$result['result'] = false; // or true depending on validation
$result['message'] = "Message to Display to the User when False";

GuiControls and Forms both use integrate the use of validator. Simple by defining the validate parameters of either, you will be using validator.

There are also a handful of special functions to help you in implementing validation in your code.

Special Functions

Validate

validate wrapper for the functions in this class.

The validate array must have 'type' => $type set for this to work. The type is any found in the above table (and php able).

This function also permits 'required' as a parameter in the array(). Without required set to true, a value will validate as true if it is either empty, null, or validates.

Example:

Validator::validate($value, array('type' => 'email', 'required' => 'true'));

boolValidate

validate wrapper for the functions in this class. Works like validate, but returns a boolean (true / false) value rather than an array.

The validate array must have 'type' => $type set for this to work. The type is any found in the above table (and php able).

Example:

Validator::boolValidate($value, array('type' => 'email'));

validateStack

This function will validate more than one validator, stacked in order one at a time. Permits validating things like Alpahnumeric & Length together for example.

It will return as soon as one of the validators fails. This is particularly useful if one of the validators performs a sql check.

The validate array must have 'validators' => array of individual "validate" arrays  set for this to work.validate wrapper for the functions in this class.

Example:

Validator::validateStack($value, array('required' => true, 
   'validators' =>
      array(
	array('type' => 'AlphaNumeric'),
	array('type' => 'length', "min" => 4, "max" => 20),
	array('type' => 'dbUnique', 'field' => 'username', 'table' => 'users')
 )));

validateMerge

This function will validate more than one validator, stacked in order one at a time. Permits validating things like Alpahnumeric & Length together for example.

It will perform all validations before it returns the combined results.

The validate array must have 'validators' => array of individual "validate" arrays  set for this to work.validate wrapper for the functions in this class.

Example:

Validator::validateMerge($value, array('required' => true, 
   'validators' =>
      array(
	array('type' => 'AlphaNumeric'),
	array('type' => 'length', "min" => 4, "max" => 20),
	array('type' => 'dbUnique', 'field' => 'username', 'table' => 'users')
 )));