Project Versions

Table Of Contents

Previous topic

Contextual Escaping

Next topic

Forms

This Page

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:

use Phalcon\Validation\Validator,
        Phalcon\Validation\ValidatorInterface,
        Phalcon\Validation\Message;

class IpValidator extends Validator implements ValidatorInterface
{

        /**
         * Executes the validation
         *
         * @param Phalcon\Validation $validator
         * @param string $attribute
         * @return boolean
         */
        public function validate($validator, $attribute)
        {
                $value = $validator->getValue($attribute);

                if (filter_var($value, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED))) {

                        $message = $this->getOption('message');
                        if (!$message) {
                                $message = 'The IP is not valid';
                        }

                        $validator->appendMessage(new Message($message, $attribute, 'Ip'));

                        return false;
                }

                return true;
        }

}

Validation Messages

Phalcon\Validation has a messaging subsystem that provides a flexible way to output or store the validation messages generated during the validation processes.

Each message consists of an instance of the class Phalcon\Validation\Message. The set of messages generated can be retrieved with the method getMessages(). Each message provides extended information like the attribute that generated the message or the message type:

<?php

$messages = $validation->validate();
if (count($messages)) {
    foreach ($validation->getMessages() as $message) {
        echo "Message: ", $message->getMessage(), "\n";
        echo "Field: ", $message->getField(), "\n";
        echo "Type: ", $message->getType(), "\n";
    }
}

The method getMessages() can be overriden in a validation class to replace/translate the default messages generated automatically by the validators:

<?php

class MyValidation extends Phalcon\Validation
{
    public function getMessages()
    {
        $messages = array();
        foreach (parent::getMessages() as $message) {
            switch ($message->getType()) {
                case 'PresenceOf':
                    $messages[] = 'The field ' . $message->getField() . ' is mandatory';
                    break;
            }
        }
        return $messages;
    }
}

Or you can pass a parameter ‘message’ to change the default message in each validator:

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