Sections

Class Phalcon\Validation

Source on GitHub

Namespace Phalcon   Uses Phalcon\Di\DiInterface, Phalcon\Di\Injectable, Phalcon\Filter\FilterInterface, Phalcon\Messages\MessageInterface, Phalcon\Messages\Messages, Phalcon\Validation\ValidationInterface, Phalcon\Validation\Exception, Phalcon\Validation\ValidatorInterface, Phalcon\Validation\AbstractCombinedFieldsValidator   Extends Injectable   Implements ValidationInterface

Allows to validate data using custom or built-in validators

Properties

//
protected combinedFieldsValidators;

//
protected data;

//
protected entity;

//
protected filters;

//
protected labels;

//
protected messages;

//
protected validators;

//
protected values;

Methods

public function __construct( array $validators = [] );

Phalcon\Validation constructor

public function add( mixed $field, ValidatorInterface $validator ): ValidationInterface;

Adds a validator to a field

public function appendMessage( MessageInterface $message ): ValidationInterface;

Appends a message to the messages list

public function bind( mixed $entity, mixed $data ): ValidationInterface;

Assigns the data to an entity The entity is used to obtain the validation values

public function getData()
public function getEntity(): object;

Returns the bound entity

public function getFilters( string $field = null ): mixed | null;

Returns all the filters or a specific one

public function getLabel( mixed $field ): string;

Get label for field

public function getMessages(): Messages;

Returns the registered validators

public function getValidators(): array;

Returns the validators added to the validation

public function getValue( string $field ): mixed | null;

Gets the a value to validate in the array/object data source

public function rule( mixed $field, ValidatorInterface $validator ): ValidationInterface;

Alias of add method

public function rules( mixed $field, array $validators ): ValidationInterface;

Adds the validators to a field

public function setEntity( mixed $entity ): void;

Sets the bound entity

public function setFilters( mixed $field, mixed $filters ): ValidationInterface;

Adds filters to the field

public function setLabels( array $labels ): void;

Adds labels for fields

public function setValidators( $validators )
public function validate( mixed $data = null, mixed $entity = null ): Messages;

Validate a set of data according to a set of rules

protected function preChecking( mixed $field, ValidatorInterface $validator ): bool;

Internal validations, if it returns true, then skip the current validator

Abstract Class Phalcon\Validation\AbstractCombinedFieldsValidator

Source on GitHub

Namespace Phalcon\Validation   Extends AbstractValidator

This is a base class for combined fields validators

Abstract Class Phalcon\Validation\AbstractValidator

Source on GitHub

Namespace Phalcon\Validation   Uses Phalcon\Collection, Phalcon\Helper\Arr, Phalcon\Messages\Message, Phalcon\Validation   Implements ValidatorInterface

This is a base class for validators

Properties

/**
    * Message template
    *
    * @var string|null
    */
protected template;

/**
    * Message templates
    *
    * @var array
    */
protected templates;

//
protected options;

Methods

public function __construct( array $options = [] );

Phalcon\Validation\Validator constructor

public function getOption( string $key, mixed $defaultValue = null ): mixed;

Returns an option in the validator’s options Returns null if the option hasn’t set

public function getTemplate( string $field = null ): string;

Get the template message

@return string @throw InvalidArgumentException When the field does not exists

public function getTemplates(): array;

Get templates collection object

@return array

public function hasOption( string $key ): bool;

Checks if an option is defined

public function messageFactory( Validation $validation, mixed $field, array $replacements = [] ): Message;

Create a default message by factory

@return Message

@throw Exception

public function setOption( string $key, mixed $value ): void;

Sets an option in the validator

public function setTemplate( string $template ): ValidatorInterface;

Set a new template message

@return ValidatorInterface

public function setTemplates( array $templates ): ValidatorInterface;

Clear current templates and set new from an array,

@return ValidatorInterface

abstract public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

protected function prepareCode( string $field ): int | null;

Prepares a validation code.

protected function prepareLabel( Validation $validation, string $field ): mixed;

Prepares a label for the field.

Abstract Class Phalcon\Validation\AbstractValidatorComposite

Source on GitHub

Namespace Phalcon\Validation   Uses Phalcon\Validation   Extends AbstractValidator   Implements ValidatorCompositeInterface

This is a base class for combined fields validators

Properties

/**
 * @var array
 */
protected validators;

Methods

public function getValidators(): array
public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Exception

Source on GitHub

Namespace Phalcon\Validation   Extends \Phalcon\Exception

Exceptions thrown in Phalcon\Validation* classes will use this class

Interface Phalcon\Validation\ValidationInterface

Source on GitHub

Namespace Phalcon\Validation   Uses Phalcon\Di\Injectable, Phalcon\Messages\MessageInterface, Phalcon\Messages\Messages

Interface for the Phalcon\Validation component

Methods

public function add( string $field, ValidatorInterface $validator ): ValidationInterface;

Adds a validator to a field

public function appendMessage( MessageInterface $message ): ValidationInterface;

Appends a message to the messages list

public function bind( mixed $entity, mixed $data ): ValidationInterface;

Assigns the data to an entity The entity is used to obtain the validation values

public function getEntity(): object;

Returns the bound entity

public function getFilters( string $field = null ): mixed | null;

Returns all the filters or a specific one

public function getLabel( string $field ): string;

Get label for field

public function getMessages(): Messages;

Returns the registered validators

public function getValidators(): array;

Returns the validators added to the validation

public function getValue( string $field ): mixed | null;

Gets the a value to validate in the array/object data source

public function rule( string $field, ValidatorInterface $validator ): ValidationInterface;

Alias of add method

public function rules( string $field, array $validators ): ValidationInterface;

Adds the validators to a field

public function setFilters( string $field, mixed $filters ): ValidationInterface;

Adds filters to the field

public function setLabels( array $labels ): void;

Adds labels for fields

public function validate( mixed $data = null, mixed $entity = null ): Messages;

Validate a set of data according to a set of rules

Class Phalcon\Validation\Validator\Alnum

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Check for alphanumeric character(s)

use Phalcon\Validation;
use Phalcon\Validation\Validator\Alnum as AlnumValidator;

$validator = new Validation();

$validator->add(
    "username",
    new AlnumValidator(
        [
            "message" => ":field must contain only alphanumeric characters",
        ]
    )
);

$validator->add(
    [
        "username",
        "name",
    ],
    new AlnumValidator(
        [
            "message" => [
                "username" => "username must contain only alphanumeric characters",
                "name"     => "name must contain only alphanumeric characters",
            ],
        ]
    )
);

Properties

//
protected template = Field :field must contain only letters and numbers;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Alpha

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Check for alphabetic character(s)

use Phalcon\Validation;
use Phalcon\Validation\Validator\Alpha as AlphaValidator;

$validator = new Validation();

$validator->add(
    "username",
    new AlphaValidator(
        [
            "message" => ":field must contain only letters",
        ]
    )
);

$validator->add(
    [
        "username",
        "name",
    ],
    new AlphaValidator(
        [
            "message" => [
                "username" => "username must contain only letters",
                "name"     => "name must contain only letters",
            ],
        ]
    )
);

Properties

//
protected template = Field :field must contain only letters;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Between

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Validates that a value is between an inclusive range of two values. For a value x, the test is passed if minimum<=x<=maximum.

use Phalcon\Validation;
use Phalcon\Validation\Validator\Between;

$validator = new Validation();

$validator->add(
    "price",
    new Between(
        [
            "minimum" => 0,
            "maximum" => 100,
            "message" => "The price must be between 0 and 100",
        ]
    )
);

$validator->add(
    [
        "price",
        "amount",
    ],
    new Between(
        [
            "minimum" => [
                "price"  => 0,
                "amount" => 0,
            ],
            "maximum" => [
                "price"  => 100,
                "amount" => 50,
            ],
            "message" => [
                "price"  => "The price must be between 0 and 100",
                "amount" => "The amount must be between 0 and 50",
            ],
        ]
    )
);

Properties

//
protected template = Field :field must be within the range of :min to :max;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Callback

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\ValidatorInterface, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Calls user function for validation

use Phalcon\Validation;
use Phalcon\Validation\Validator\Callback as CallbackValidator;
use Phalcon\Validation\Validator\Numericality as NumericalityValidator;

$validator = new Validation();

$validator->add(
    ["user", "admin"],
    new CallbackValidator(
        [
            "message" => "There must be only an user or admin set",
            "callback" => function($data) {
                if (!empty($data->getUser()) && !empty($data->getAdmin())) {
                    return false;
                }

                return true;
            }
        ]
    )
);

$validator->add(
    "amount",
    new CallbackValidator(
        [
            "callback" => function($data) {
                if (!empty($data->getProduct())) {
                    return new NumericalityValidator(
                        [
                            "message" => "Amount must be a number."
                        ]
                    );
                }
            }
        ]
    )
);

Properties

//
protected template = Field :field must match the callback function;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Confirmation

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\Exception, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Checks that two values have the same value

use Phalcon\Validation;
use Phalcon\Validation\Validator\Confirmation;

$validator = new Validation();

$validator->add(
    "password",
    new Confirmation(
        [
            "message" => "Password doesn't match confirmation",
            "with"    => "confirmPassword",
        ]
    )
);

$validator->add(
    [
        "password",
        "email",
    ],
    new Confirmation(
        [
            "message" => [
                "password" => "Password doesn't match confirmation",
                "email"    => "Email doesn't match confirmation",
            ],
            "with" => [
                "password" => "confirmPassword",
                "email"    => "confirmEmail",
            ],
        ]
    )
);

Properties

//
protected template = Field :field must be the same as :with;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

final protected function compare( string $a, string $b ): bool;

Compare strings

Class Phalcon\Validation\Validator\CreditCard

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Checks if a value has a valid credit card number

use Phalcon\Validation;
use Phalcon\Validation\Validator\CreditCard as CreditCardValidator;

$validator = new Validation();

$validator->add(
    "creditCard",
    new CreditCardValidator(
        [
            "message" => "The credit card number is not valid",
        ]
    )
);

$validator->add(
    [
        "creditCard",
        "secondCreditCard",
    ],
    new CreditCardValidator(
        [
            "message" => [
                "creditCard"       => "The credit card number is not valid",
                "secondCreditCard" => "The second credit card number is not valid",
            ],
        ]
    )
);

Properties

//
protected template = Field :field is not valid for a credit card number;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Date

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses DateTime, Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Checks if a value is a valid date

use Phalcon\Validation;
use Phalcon\Validation\Validator\Date as DateValidator;

$validator = new Validation();

$validator->add(
    "date",
    new DateValidator(
        [
            "format"  => "d-m-Y",
            "message" => "The date is invalid",
        ]
    )
);

$validator->add(
    [
        "date",
        "anotherDate",
    ],
    new DateValidator(
        [
            "format" => [
                "date"        => "d-m-Y",
                "anotherDate" => "Y-m-d",
            ],
            "message" => [
                "date"        => "The date is invalid",
                "anotherDate" => "The another date is invalid",
            ],
        ]
    )
);

Properties

//
protected template = Field :field is not a valid date;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Digit

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Check for numeric character(s)

use Phalcon\Validation;
use Phalcon\Validation\Validator\Digit as DigitValidator;

$validator = new Validation();

$validator->add(
    "height",
    new DigitValidator(
        [
            "message" => ":field must be numeric",
        ]
    )
);

$validator->add(
    [
        "height",
        "width",
    ],
    new DigitValidator(
        [
            "message" => [
                "height" => "height must be numeric",
                "width"  => "width must be numeric",
            ],
        ]
    )
);

Properties

//
protected template = Field :field must be numeric;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Email

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Checks if a value has a correct e-mail format

use Phalcon\Validation;
use Phalcon\Validation\Validator\Email as EmailValidator;

$validator = new Validation();

$validator->add(
    "email",
    new EmailValidator(
        [
            "message" => "The e-mail is not valid",
        ]
    )
);

$validator->add(
    [
        "email",
        "anotherEmail",
    ],
    new EmailValidator(
        [
            "message" => [
                "email"        => "The e-mail is not valid",
                "anotherEmail" => "The another e-mail is not valid",
            ],
        ]
    )
);

Properties

//
protected template = Field :field must be an email address;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Exception

Source on GitHub

Namespace Phalcon\Validation\Validator   Extends \Phalcon\Exception

Exceptions thrown in Phalcon\Validation\Validator* classes will use this class

Class Phalcon\Validation\Validator\ExclusionIn

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator, Phalcon\Validation\Exception   Extends AbstractValidator

Check if a value is not included into a list of values

use Phalcon\Validation;
use Phalcon\Validation\Validator\ExclusionIn;

$validator = new Validation();

$validator->add(
    "status",
    new ExclusionIn(
        [
            "message" => "The status must not be A or B",
            "domain"  => [
                "A",
                "B",
            ],
        ]
    )
);

$validator->add(
    [
        "status",
        "type",
    ],
    new ExclusionIn(
        [
            "message" => [
                "status" => "The status must not be A or B",
                "type"   => "The type must not be 1 or "
            ],
            "domain" => [
                "status" => [
                    "A",
                    "B",
                ],
                "type"   => [1, 2],
            ],
        ]
    )
);

Properties

//
protected template = Field :field must not be a part of list: :domain;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\File

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Helper\Arr, Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidatorComposite, Phalcon\Validation\Validator\File\MimeType, Phalcon\Validation\Validator\File\Resolution\Equal, Phalcon\Validation\Validator\File\Resolution\Max, Phalcon\Validation\Validator\File\Resolution\Min, Phalcon\Validation\Validator\File\Size\Equal, Phalcon\Validation\Validator\File\Size\Max, Phalcon\Validation\Validator\File\Size\Min   Extends AbstractValidatorComposite

Checks if a value has a correct file

use Phalcon\Validation;
use Phalcon\Validation\Validator\File as FileValidator;

$validator = new Validation();

$validator->add(
    "file",
    new FileValidator(
        [
            "maxSize"              => "2M",
            "messageSize"          => ":field exceeds the max filesize (:size)",
            "allowedTypes"         => [
                "image/jpeg",
                "image/png",
            ],
            "messageType"          => "Allowed file types are :types",
            "maxResolution"        => "800x600",
            "messageMaxResolution" => "Max resolution of :field is :resolution",
        ]
    )
);

$validator->add(
    [
        "file",
        "anotherFile",
    ],
    new FileValidator(
        [
            "maxSize" => [
                "file"        => "2M",
                "anotherFile" => "4M",
            ],
            "messageSize" => [
                "file"        => "file exceeds the max filesize 2M",
                "anotherFile" => "anotherFile exceeds the max filesize 4M",
            "allowedTypes" => [
                "file"        => [
                    "image/jpeg",
                    "image/png",
                ],
                "anotherFile" => [
                    "image/gif",
                    "image/bmp",
                ],
            ],
            "messageType" => [
                "file"        => "Allowed file types are image/jpeg and image/png",
                "anotherFile" => "Allowed file types are image/gif and image/bmp",
            ],
            "maxResolution" => [
                "file"        => "800x600",
                "anotherFile" => "1024x768",
            ],
            "messageMaxResolution" => [
                "file"        => "Max resolution of file is 800x600",
                "anotherFile" => "Max resolution of file is 1024x768",
            ],
        ]
    )
);

Methods

public function __construct( array $options = [] );

Constructor

Abstract Class Phalcon\Validation\Validator\File\AbstractFile

Source on GitHub

Namespace Phalcon\Validation\Validator\File   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Checks if a value has a correct file

use Phalcon\Validation;
use Phalcon\Validation\Validator\File\Size;

$validator = new Validation();

$validator->add(
    "file",
    new Size(
        [
            "maxSize"              => "2M",
            "messageSize"          => ":field exceeds the max filesize (:size)",
        ]
    )
);

$validator->add(
    [
        "file",
        "anotherFile",
    ],
    new FileValidator(
        [
            "maxSize" => [
                "file"        => "2M",
                "anotherFile" => "4M",
            ],
            "messageSize" => [
                "file"        => "file exceeds the max filesize 2M",
                "anotherFile" => "anotherFile exceeds the max filesize 4M",
            ],
        ]
    )
);

Properties

/**
    * Empty is empty
    */
protected messageFileEmpty = Field :field must not be empty;

/**
    * File exceeed the file size setted in PHP configuration
    */
protected messageIniSize = File :field exceeds the maximum file size;

/**
    * File is not valid
    */
protected messageValid = Field :field is not valid;

Methods

public function checkUpload( Validation $validation, mixed $field ): bool;

Check upload

     @return bool
public function checkUploadIsEmpty( Validation $validation, mixed $field ): bool;

Check if upload is empty

     @return boolean
public function checkUploadIsValid( Validation $validation, mixed $field ): bool;

Check if upload is valid

     @return boolean
public function checkUploadMaxSize( Validation $validation, mixed $field ): bool;

Check if uploaded file is larger than PHP allowed size

     @return boolean
public function getFileSizeInBytes( string $size ): double;

Convert a string like “2.5MB” in bytes

  @return float
public function getMessageFileEmpty()
public function getMessageIniSize()
public function getMessageValid()
public function isAllowEmpty( Validation $validation, string $field ): bool;

Check on empty

@return bool

public function setMessageFileEmpty( $messageFileEmpty )
public function setMessageIniSize( $messageIniSize )
public function setMessageValid( $messageValid )

Class Phalcon\Validation\Validator\File\MimeType

Source on GitHub

Namespace Phalcon\Validation\Validator\File   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\Exception   Extends AbstractFile

Checks if a value has a correct file mime type

use Phalcon\Validation;
use Phalcon\Validation\Validator\File\MimeType;

$validator = new Validation();

$validator->add(
    "file",
    new MimeType(
        [
            "types" => [
                "image/jpeg",
                "image/png",
            ],
            "message" => "Allowed file types are :types"
        ]
    )
);

$validator->add(
    [
        "file",
        "anotherFile",
    ],
    new MimeType(
        [
            "types" => [
                "file"        => [
                    "image/jpeg",
                    "image/png",
                ],
                "anotherFile" => [
                    "image/gif",
                    "image/bmp",
                ],
            ],
            "message" => [
                "file"        => "Allowed file types are image/jpeg and image/png",
                "anotherFile" => "Allowed file types are image/gif and image/bmp",
            ]
        ]
    )
);

Properties

//
protected template = File :field must be of type: :types;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

@return bool

Class Phalcon\Validation\Validator\File\Resolution\Equal

Source on GitHub

Namespace Phalcon\Validation\Validator\File\Resolution   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\Validator\File\AbstractFile   Extends AbstractFile

Checks if a file has the rigth resolution

use Phalcon\Validation;
use Phalcon\Validation\Validator\File\Resolution\Equal;

$validator = new Validation();

$validator->add(
    "file",
    new Equal(
        [
            "resolution" => "800x600",
            "message"    => "The resolution of the field :field has to be equal :resolution",
        ]
    )
);

$validator->add(
    [
        "file",
        "anotherFile",
    ],
    new Equal(
        [
            "resolution" => [
                "file"        => "800x600",
                "anotherFile" => "1024x768",
            ],
            "message" => [
                "file"        => "Equal resolution of file has to be 800x600",
                "anotherFile" => "Equal resolution of file has to be 1024x768",
            ],
        ]
    )
);

Properties

//
protected template = The resolution of the field :field has to be equal :resolution;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\File\Resolution\Max

Source on GitHub

Namespace Phalcon\Validation\Validator\File\Resolution   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\Validator\File\AbstractFile   Extends AbstractFile

Checks if a file has the rigth resolution

use Phalcon\Validation;
use Phalcon\Validation\Validator\File\Resolution\Max;

$validator = new Validation();

$validator->add(
    "file",
    new Max(
        [
            "resolution"      => "800x600",
            "message"  => "Max resolution of :field is :resolution",
            "included" => true,
        ]
    )
);

$validator->add(
    [
        "file",
        "anotherFile",
    ],
    new Max(
        [
            "resolution" => [
                "file"        => "800x600",
                "anotherFile" => "1024x768",
            ],
            "included" => [
                "file"        => false,
                "anotherFile" => true,
            ],
            "message" => [
                "file"        => "Max resolution of file is 800x600",
                "anotherFile" => "Max resolution of file is 1024x768",
            ],
        ]
    )
);

Properties

//
protected template = File :field exceeds the maximum resolution of :resolution;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\File\Resolution\Min

Source on GitHub

Namespace Phalcon\Validation\Validator\File\Resolution   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\Validator\File\AbstractFile   Extends AbstractFile

Checks if a file has the rigth resolution

use Phalcon\Validation;
use Phalcon\Validation\Validator\File\Resolution\Min;

$validator = new Validation();

$validator->add(
    "file",
    new Min(
        [
            "resolution" => "800x600",
            "message"    => "Min resolution of :field is :resolution",
            "included"   => true,
        ]
    )
);

$validator->add(
    [
        "file",
        "anotherFile",
    ],
    new Min(
        [
            "resolution" => [
                "file"        => "800x600",
                "anotherFile" => "1024x768",
            ],
            "included" => [
                "file"        => false,
                "anotherFile" => true,
            ],
            "message" => [
                "file"        => "Min resolution of file is 800x600",
                "anotherFile" => "Min resolution of file is 1024x768",
            ],
        ]
    )
);

Properties

//
protected template = File :field can not have the minimum resolution of :resolution;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\File\Size\Equal

Source on GitHub

Namespace Phalcon\Validation\Validator\File\Size   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\Validator\File\AbstractFile   Extends AbstractFile

Checks if a value has a correct file

use Phalcon\Validation;
use Phalcon\Validation\Validator\File\Size;

$validator = new Validation();

$validator->add(
    "file",
    new Equal(
        [
            "size"     => "2M",
            "included" => true,
            "message"  => ":field exceeds the equal filesize (:size)",
        ]
    )
);

$validator->add(
    [
        "file",
        "anotherFile",
    ],
    new Equal(
        [
            "size" => [
                "file"        => "2M",
                "anotherFile" => "4M",
            ],
            "included" => [
                "file"        => false,
                "anotherFile" => true,
            ],
            "message" => [
                "file"        => "file does not have the rigth filesize",
                "anotherFile" => "anotherFile wrong filesize (4MB)",
            ],
        ]
    )
);

Properties

//
protected template = File :field does not have the exact :size filesize;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\File\Size\Max

Source on GitHub

Namespace Phalcon\Validation\Validator\File\Size   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\Validator\File\AbstractFile   Extends AbstractFile

Checks if a value has a correct file

use Phalcon\Validation;
use Phalcon\Validation\Validator\File\Size;

$validator = new Validation();

$validator->add(
    "file",
    new Max(
        [
            "size"     => "2M",
            "included" => true,
            "message"  => ":field exceeds the max filesize (:size)",
        ]
    )
);

$validator->add(
    [
        "file",
        "anotherFile",
    ],
    new Max(
        [
            "size" => [
                "file"        => "2M",
                "anotherFile" => "4M",
            ],
            "included" => [
                "file"        => false,
                "anotherFile" => true,
            ],
            "message" => [
                "file"        => "file exceeds the max filesize 2M",
                "anotherFile" => "anotherFile exceeds the max filesize 4M",
            ],
        ]
    )
);

Properties

//
protected template = File :field exceeds the size of :size;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\File\Size\Min

Source on GitHub

Namespace Phalcon\Validation\Validator\File\Size   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\Validator\File\AbstractFile   Extends AbstractFile

Checks if a value has a correct file

use Phalcon\Validation;
use Phalcon\Validation\Validator\File\Size;

$validator = new Validation();

$validator->add(
    "file",
    new Min(
        [
            "size"     => "2M",
            "included" => true,
            "message"  => ":field exceeds the min filesize (:size)",
        ]
    )
);

$validator->add(
    [
        "file",
        "anotherFile",
    ],
    new Min(
        [
            "size" => [
                "file"        => "2M",
                "anotherFile" => "4M",
            ],
            "included" => [
                "file"        => false,
                "anotherFile" => true,
            ],
            "message" => [
                "file"        => "file exceeds the min filesize 2M",
                "anotherFile" => "anotherFile exceeds the min filesize 4M",
            ],
        ]
    )
);

Properties

//
protected template = File :field can not have the minimum size of :size;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Identical

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Checks if a value is identical to other

use Phalcon\Validation;
use Phalcon\Validation\Validator\Identical;

$validator = new Validation();

$validator->add(
    "terms",
    new Identical(
        [
            "accepted" => "yes",
            "message" => "Terms and conditions must be accepted",
        ]
    )
);

$validator->add(
    [
        "terms",
        "anotherTerms",
    ],
    new Identical(
        [
            "accepted" => [
                "terms"        => "yes",
                "anotherTerms" => "yes",
            ],
            "message" => [
                "terms"        => "Terms and conditions must be accepted",
                "anotherTerms" => "Another terms  must be accepted",
            ],
        ]
    )
);

Properties

//
protected template = Field :field does not have the expected value;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\InclusionIn

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator, Phalcon\Validation\Exception   Extends AbstractValidator

Check if a value is included into a list of values

use Phalcon\Validation;
use Phalcon\Validation\Validator\InclusionIn;

$validator = new Validation();

$validator->add(
    "status",
    new InclusionIn(
        [
            "message" => "The status must be A or B",
            "domain"  => ["A", "B"],
        ]
    )
);

$validator->add(
    [
        "status",
        "type",
    ],
    new InclusionIn(
        [
            "message" => [
                "status" => "The status must be A or B",
                "type"   => "The status must be 1 or 2",
            ],
            "domain" => [
                "status" => ["A", "B"],
                "type"   => [1, 2],
            ]
        ]
    )
);

Properties

//
protected template = Field :field must be a part of list: :domain;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Ip

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Validation, Phalcon\Validation\AbstractValidator, Phalcon\Messages\Message   Extends AbstractValidator

Check for IP addresses

use Phalcon\Validation\Validator\Ip as IpValidator;

$validator->add(
    "ip_address",
    new IpValidator(
        [
            "message"       => ":field must contain only ip addresses",
            "version"       => IP::VERSION_4 | IP::VERSION_6, // v6 and v4. The same if not specified
            "allowReserved" => false,   // False if not specified. Ignored for v6
            "allowPrivate"  => false,   // False if not specified
            "allowEmpty"    => false,
        ]
    )
);

$validator->add(
    [
        "source_address",
        "destination_address",
    ],
    new IpValidator(
        [
            "message" => [
                "source_address"      => "source_address must be a valid IP address",
                "destination_address" => "destination_address must be a valid IP address",
            ],
            "version" => [
                 "source_address"      => Ip::VERSION_4 | IP::VERSION_6,
                 "destination_address" => Ip::VERSION_4,
            ],
            "allowReserved" => [
                 "source_address"      => false,
                 "destination_address" => true,
            ],
            "allowPrivate" => [
                 "source_address"      => false,
                 "destination_address" => true,
            ],
            "allowEmpty" => [
                 "source_address"      => false,
                 "destination_address" => true,
            ],
        ]
    )
);

Constants

const VERSION_4 = FILTER_FLAG_IPV4;
const VERSION_6 = FILTER_FLAG_IPV6;

Properties

//
protected template = Field :field must be a valid IP address;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Numericality

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Check for a valid numeric value

use Phalcon\Validation;
use Phalcon\Validation\Validator\Numericality;

$validator = new Validation();

$validator->add(
    "price",
    new Numericality(
        [
            "message" => ":field is not numeric",
        ]
    )
);

$validator->add(
    [
        "price",
        "amount",
    ],
    new Numericality(
        [
            "message" => [
                "price"  => "price is not numeric",
                "amount" => "amount is not numeric",
            ]
        ]
    )
);

Properties

//
protected template = Field :field does not have a valid numeric format;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\PresenceOf

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Validates that a value is not null or empty string

use Phalcon\Validation;
use Phalcon\Validation\Validator\PresenceOf;

$validator = new Validation();

$validator->add(
    "name",
    new PresenceOf(
        [
            "message" => "The name is required",
        ]
    )
);

$validator->add(
    [
        "name",
        "email",
    ],
    new PresenceOf(
        [
            "message" => [
                "name"  => "The name is required",
                "email" => "The email is required",
            ],
        ]
    )
);

Properties

//
protected template = Field :field is required;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Regex

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Allows validate if the value of a field matches a regular expression

use Phalcon\Validation;
use Phalcon\Validation\Validator\Regex as RegexValidator;

$validator = new Validation();

$validator->add(
    "created_at",
    new RegexValidator(
        [
            "pattern" => "/^[0-9]{4}[-\/](0[1-9]|1[12])[-\/](0[1-9]|[12][0-9]|3[01])$/",
            "message" => "The creation date is invalid",
        ]
    )
);

$validator->add(
    [
        "created_at",
        "name",
    ],
    new RegexValidator(
        [
            "pattern" => [
                "created_at" => "/^[0-9]{4}[-\/](0[1-9]|1[12])[-\/](0[1-9]|[12][0-9]|3[01])$/",
                "name"       => "/^[a-z]$/",
            ],
            "message" => [
                "created_at" => "The creation date is invalid",
                "name"       => "The name is invalid",
            ]
        ]
    )
);

Properties

//
protected template = Field :field does not match the required format;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\StringLength

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation\AbstractValidator, Phalcon\Validation\AbstractValidatorComposite, Phalcon\Validation\Validator\StringLength\Max, Phalcon\Validation\Validator\StringLength\Min, Phalcon\Validation\Exception   Extends AbstractValidatorComposite

Validates that a string has the specified maximum and minimum constraints The test is passed if for a string’s length L, min<=L<=max, i.e. L must be at least min, and at most max. Since Phalcon v4.0 this valitor works like a container

use Phalcon\Validation;
use Phalcon\Validation\Validator\StringLength as StringLength;

$validator = new Validation();

$validation->add(
    "name_last",
    new StringLength(
        [
            "max"             => 50,
            "min"             => 2,
            "messageMaximum"  => "We don't like really long names",
            "messageMinimum"  => "We want more than just their initials",
            "includedMaximum" => true,
            "includedMinimum" => false,
        ]
    )
);

$validation->add(
    [
        "name_last",
        "name_first",
    ],
    new StringLength(
        [
            "max" => [
                "name_last"  => 50,
                "name_first" => 40,
            ],
            "min" => [
                "name_last"  => 2,
                "name_first" => 4,
            ],
            "messageMaximum" => [
                "name_last"  => "We don't like really long last names",
                "name_first" => "We don't like really long first names",
            ],
            "messageMinimum" => [
                "name_last"  => "We don't like too short last names",
                "name_first" => "We don't like too short first names",
            ],
            "includedMaximum" => [
                "name_last"  => false,
                "name_first" => true,
            ],
            "includedMinimum" => [
                "name_last"  => false,
                "name_first" => true,
            ]
        ]
    )
);

Methods

public function __construct( array $options = [] );

Constructor

Class Phalcon\Validation\Validator\StringLength\Max

Source on GitHub

Namespace Phalcon\Validation\Validator\StringLength   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator, Phalcon\Validation\Exception   Extends AbstractValidator

Validates that a string has the specified maximum constraints The test is passed if for a string’s length L, L<=max, i.e. L must be at most max.

use Phalcon\Validation;
use Phalcon\Validation\Validator\StringLength\Max;

$validator = new Validation();

$validation->add(
    "name_last",
    new Max(
        [
            "max"      => 50,
            "message"  => "We don't like really long names",
            "included" => true
        ]
    )
);

$validation->add(
    [
        "name_last",
        "name_first",
    ],
    new Max(
        [
            "max" => [
                "name_last"  => 50,
                "name_first" => 40,
            ],
            "message" => [
                "name_last"  => "We don't like really long last names",
                "name_first" => "We don't like really long first names",
            ],
            "included" => [
                "name_last"  => false,
                "name_first" => true,
            ]
        ]
    )
);

Properties

//
protected template = Field :field must not exceed :max characters long;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\StringLength\Min

Source on GitHub

Namespace Phalcon\Validation\Validator\StringLength   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator, Phalcon\Validation\Exception   Extends AbstractValidator

Validates that a string has the specified minimum constraints The test is passed if for a string’s length L, min<=L, i.e. L must be at least min.

use Phalcon\Validation;
use Phalcon\Validation\Validator\StringLength\Min;

$validator = new Validation();

$validation->add(
    "name_last",
    new Min(
        [
            "min"     => 2,
            "message" => "We want more than just their initials",
            "included" => true
        ]
    )
);

$validation->add(
    [
        "name_last",
        "name_first",
    ],
    new Min(
        [
            "min" => [
                "name_last"  => 2,
                "name_first" => 4,
            ],
            "message" => [
                "name_last"  => "We don't like too short last names",
                "name_first" => "We don't like too short first names",
            ],
            "included" => [
                "name_last"  => false,
                "name_first" => true,
            ]
        ]
    )
);

Properties

//
protected template = Field :field must be at least :min characters long;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\Validator\Uniqueness

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Mvc\Model, Phalcon\Mvc\ModelInterface, Phalcon\Validation, Phalcon\Validation\AbstractCombinedFieldsValidator, Phalcon\Validation\Exception   Extends AbstractCombinedFieldsValidator

Check that a field is unique in the related table

use Phalcon\Validation;
use Phalcon\Validation\Validator\Uniqueness as UniquenessValidator;

$validator = new Validation();

$validator->add(
    "username",
    new UniquenessValidator(
        [
            "model"   => new Users(),
            "message" => ":field must be unique",
        ]
    )
);

Different attribute from the field:

$validator->add(
    "username",
    new UniquenessValidator(
        [
            "model"     => new Users(),
            "attribute" => "nick",
        ]
    )
);

In model:

$validator->add(
    "username",
    new UniquenessValidator()
);

Combination of fields in model:

$validator->add(
    [
        "firstName",
        "lastName",
    ],
    new UniquenessValidator()
);

It is possible to convert values before validation. This is useful in situations where values need to be converted to do the database lookup:

$validator->add(
    "username",
    new UniquenessValidator(
        [
            "convert" => function (array $values) {
                $values["username"] = strtolower($values["username"]);

                return $values;
            }
        ]
    )
);

Properties

//
protected template = Field :field must be unique;

//
private columnMap;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

protected function getColumnNameReal( mixed $record, string $field ): string;

The column map is used in the case to get real column name

protected function isUniqueness( Validation $validation, mixed $field ): bool;

//

protected function isUniquenessModel( mixed $record, array $field, array $values );

Uniqueness method used for model

Class Phalcon\Validation\Validator\Url

Source on GitHub

Namespace Phalcon\Validation\Validator   Uses Phalcon\Messages\Message, Phalcon\Validation, Phalcon\Validation\AbstractValidator   Extends AbstractValidator

Checks if a value has a url format

use Phalcon\Validation;
use Phalcon\Validation\Validator\Url as UrlValidator;

$validator = new Validation();

$validator->add(
    "url",
    new UrlValidator(
        [
            "message" => ":field must be a url",
        ]
    )
);

$validator->add(
    [
        "url",
        "homepage",
    ],
    new UrlValidator(
        [
            "message" => [
                "url"      => "url must be a url",
                "homepage" => "homepage must be a url",
            ]
        ]
    )
);

Properties

//
protected template = Field :field must be a url;

Methods

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Interface Phalcon\Validation\ValidatorCompositeInterface

Source on GitHub

Namespace Phalcon\Validation   Uses Phalcon\Validation

This is a base class for combined fields validators

Methods

public function getValidators(): array;

Executes the validation

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

Class Phalcon\Validation\ValidatorFactory

Source on GitHub

Namespace Phalcon\Validation   Uses Phalcon\Factory\AbstractFactory, Phalcon\Factory\ValidatorInterface, Phalcon\Helper\Arr   Extends AbstractFactory

This file is part of the Phalcon Framework.

(c) Phalcon Team [email protected]

For the full copyright and license information, please view the LICENSE.txt file that was distributed with this source code.

Methods

public function __construct( array $services = [] );

TagFactory constructor.

public function newInstance( string $name ): ValidatorInterface;

Creates a new instance

protected function getAdapters(): array;

//

Interface Phalcon\Validation\ValidatorInterface

Source on GitHub

Namespace Phalcon\Validation   Uses Phalcon\Collection, Phalcon\Validation

Interface for Phalcon\Validation\AbstractValidator

Methods

public function getOption( string $key, mixed $defaultValue = null ): mixed;

Returns an option in the validator’s options Returns null if the option hasn’t set

@return mixed

public function getTemplate( string $field ): string;

Get the template message

@return string @throw InvalidArgumentException When the field does not exists

public function getTemplates(): array;

Get message templates

@return array

public function hasOption( string $key ): bool;

Checks if an option is defined

@return boolean

public function setTemplate( string $template ): ValidatorInterface;

Set a new temlate message

@return ValidatorInterface

public function setTemplates( array $templates ): ValidatorInterface;

Clear current template and set new from an array,

@return ValidatorInterface

public function validate( Validation $validation, mixed $field ): bool;

Executes the validation

@return boolean