Tabla de Contenidos

Tema anterior

< Contextual Escaping

Próximo tema

Forms >

Esta página

Validation

PhalconValidation is an independent validation component to validate an arbitrary set of data. This component can be used to implement validation rules that does not belong to a model or collection.

The following example shows its basic usage:

use Phalcon\Validation\Validator\PresenceOf,
        Phalcon\Validation\Validator\Email;

$validation = new Phalcon\Validation();

$validation->add('name', new PresenceOf(
        'message' => 'The name is required'
));

$validation->add('email', new PresenceOf(
        'message' => 'The e-mail is required'
));

$validation->add('email', new Email(
        'message' => 'The e-mail is not valid'
));

$messages = $validation->validate($_POST);
if (count($messages)) {
        foreach ($messages as $message) {
                echo $message, '<br>;
        }
}

Validators

Phalcon exposes a set of built-in validators for this component:

Name Explanation Example
PresenceOf Validates that a field’s value isn’t null or empty string. Example
Email Validates that field contains a valid email format Example
ExclusionIn Validates that a value is not within a list of possible values Example
InclusionIn Validates that a value is within a list of possible values Example
Regex Validates that the value of a field matches a regular expression Example
StringLength Validates the length of a string Example

Additional validators can be created by the developer. The following class explains how to create a validator for this component: