FUEL CMS User Guide : Version 0.9.3


Validator Class

The Validator class provides an alternative validation library from CodeIgniter's Form Validation Class.

Initializing the Class

Like most other classes in CodeIgniter, the Validator class is initialized in your controller using the $this->load->library function:

$this->load->library('validator');

Alternatively, you can pass initialization parameters as the second parameter:

$this->load->library('validator', array('load_helpers' => FALSE));

There is a validator_helper that will automatically get loaded with this class. This helper contains common validation functions that can be used with the Validator Class.

Why a Different Validation Class?

CodeIgniter has it's own Form Validation class. There are a couple reasons why FUEL uses the Validator class instead of the CI Validation Class:

  1. Validator is used in combination with the Form and MY_Model classes and so code control is important
  2. The CI Validation Class relies on $_POST whereas the Validator is a little more generic in that sense and does not

Configuring Validator Information

There are several public properties you can use to configure the Validator Class:

The Validator Rule

Validator rules are simple objects that have a func and msg property and a rund method. You most likely won't work with the objects directly but instead use the functions below.

Function Reference

$this->validator->add_rule('field', 'func', 'msg', 'params')

Will add a processing rule (function) to an input variable. The $field parameter will most like be the name of your form field you want to validate. The $func parameter is the name of the function to execute. The function must return TRUE or FALSE. The $msg parameter is the error message to display if the $func returns FALSE. The $params parameter are the parameters to pass to the $func. If nothing is provided, it will look for $_POST[$field]. To pass multiple arguments to the processing $func, turn $params into an array with the additional arguments being added after the value to be processed (see example below for length_max).

$posted = $_POST;
$this->validator->add_rule('name', 'required', 'The name field is required', $posted['name']);
$this->validator->add_rule('name', 'length_max', 'The name field must be no greater then 20 characters', array($posted['name'], 20));

$this->validator->remove_rule('field', 'func')

Removes a rule from a field. The $field specifies which field to remove the rule from. If a $func parameter is passed, then it will limit the removal to just that function if there happens to be multiple rules assigned to a field..

$this->validator->remove_rule('name', 'required');

$this->validator->validate()

Runs the validation rules and returns TRUE when all rules are valid and FALSE otherwise.

if ($this->validator->validate())
{
	echo 'We are valid!';
}

$this->validator->is_valid()

Returns TRUE if the validation has been run and all rules are valid and FALSE otherwise.

$this->validator->validate();
if ($this->validator->is_valid())
{
	echo 'We are valid!';
}

$this->validator->catch_error('key', 'msg')

Will catch an error message.

if ($A != $B)
{
	$this->validator->catch_error('my_field', 'A does not equal B');
}

$this->validator->catch_errors(errors)

Catches multiple errors where errors is an array of error messages.

if (!$this->my_model->save())
{
	$errors = $this->my_model->get_errors();
	$this->validator->catch_errors($errors);
}

$this->validator->get_errors()

Returns an array of error messages if they exist.

if ($A != $B)
{
	$this->validator->catch_error('my_field', 'A does not equal B');
}

foreach($this->validator->get_errors() as $error)
{
	echo $error; // 'A does not equal B'
}

$this->validator->get_last_error()

Returns an the last error if multiple errors.

if ($A != $B)
{
	$this->validator->catch_error('my_field', 'A does not equal B');
}

echo $this->validator->get_last_error(); // 'A does not equal B'

$this->validator->get_error('key')

Returns a specific error message based on the $key parameter.

if ($A != $B)
{
	$this->validator->catch_error('my_field', 'A does not equal B');
}

echo $this->validator->get_errors('my_field'); // A does not equal B

$this->validator->fields()

Returns an array of fields and rules assigned to those fields.

$fields = $this->validator->fields();
foreach($fields as $key => $field)
{
	foreach($field as $rule)
	{
		echo $rule->msg; // A does not equal B
	}
}

$this->validator->reset([reset_fields])

Resets the rules and errors of a Validator object. The optional reset_fields parameter will reset the fields to validate. The default value for reset_fields is TRUE which empties all the fields set by add_rule on the object.

$posted = $_POST;
$this->validator->add_rule('name', 'required', 'The name field is required', $posted['name']);
$this->validator->add_rule('email', 'valid_email', 'Please enter in a valid email', $posted['email']);

$fields = $this->validator->fields();
echo count($fields); // 2

$this->validator->reset();
$fields = $this->validator->fields();
echo count($fields); // 0