A class for validating array models, typically the $_POST or $_GET superglobals.

 Methods

Adds a new validation method.

addMethod(string $method_name, callable $method) 
Static

Example:

Validator::addMethod('alphanumeric', function(ValidatorContext $context, $value) {
    return $context->optional($value) || ctype_alnum($value);
});

Parameters

$method_name

string

The name of the method, i.e. 'range' or 'creditcard'.

$method

callable

The validation method, typically an anonymous function.

Validates a single field.

field(string $name) : bool

This is the rough equivalent of the 'element' method in the jQuery Validation plugin.

Example:

$validator = Validator::validate(array(...));
if ($validator->field('first_name')) {
    // The 'first_name' field is valid.
}

Parameters

$name

string

The name of the field to validate.

Returns

boolTrue if valid, false otherwise.

Returns the array of validation methods.

getMethods() : array
Static

Returns

arrayThe array containing all the validation methods.

Returns the flattened model the Validator was constructed with.

getModel() : array

Returns

arrayThe flattened model.

Returns the normalized rules the Validator was constructed with.

getRules() : array

Returns

arrayThe normalized rules.

Returns an associative array of all fields in the model that failed validation.

invalidFields() : array

Example:

$validator = Validator::validate(array(...));
foreach ($validator->invalidFields() as $name => $value) {
    error_log($name . ' did not validate.');
}

Returns

arrayAn associative array of all invalid fields.

Validates the model.

model() : bool

This is the rough equivalent of the 'form' method in the jQuery Validation plugin.

Example:

$validator = Validator::validate(array(...));
if ($validator->model()) {
    // Model validated.
}

Returns

boolTrue if the model is valid, false otherwise.

Returns the number of fields that failed validation.

numberOfInvalidFields() : int

Example:

$validator = Validator::validate(array(...));
error_log($validator->numberOfInvalidFields() . ' failed validation.');

Returns

intThe number of fields that failed validation.

Validates the model, returning a reusable Validator object in the process.

validate(array $options) : \Pajama\Validator
Static

Example (using callbacks):

Validator::validate(array(
    'model' => $_POST,
    'rules' => array(...),
    'validHandler' => function() {
        // Model validated.
    },
    'invalidHandler' => function() {
        // Model failed validation.
    },
));

Example (using methods):

$validator = Validator::validate(array(
    'model' => $_POST,
    'rules' => array(...),
));
if ($validator->model()) {
    // Model validated.
} else {
    // Model failed validation.
}

Possible options include:

  • model (required) The model to validate.
  • rules (required) The rules the model is validated against.
  • validHandler A callable that gets called if the model is valid.
  • invalidHandler A callable that gets called if the model fails validation.

Parameters

$options

array

An array of options.

Returns

\Pajama\ValidatorAn reusable Validator instance.

Creates a new Validator instance.

__construct(array $model, array $rules) 

Parameters

$model

array

The model to validate.

$rules

array

The rules the model is validated against.

Flatten an array, appending all sub-array keys into the top-level name.

flatten(array $model) : array
Static

For example, $a['foo']['bar'] = 'baz' becomes $a['foo[bar]'] = 'baz'.

Parameters

$model

array

Returns

array

Normalizes a rule, converting string rules into their array equivalents.

normalizeRule(array | string $value) : array

Parameters

$value

arraystring

Returns

array

Normalizes a rule array, removing all rules that are false.

normalizeRules(array $rules) : array

Parameters

$rules

array

Returns

array

 Properties

 

A reference to the context for this instance.

$context : \Pajama\ValidatorContext
 

An array containing all the validation methods.

$methods : array
 

The model being validated.

$model : array
 

The normalized rules for this instance.

$rules : array