How can you add validations in Phalcon?

devquora
devquora

Posted On: Feb 22, 2018

 

Phalcon uses an independent module “Phalcon\Validation” for validating users requests. This component can be used to implement validation rules on data objects that do not belong to a model or collection.
The loosely-coupled design of this component allows you to create your own validators along with the ones provided by the framework.
The following example shows basic usage of validations in Phalcon :

<?php

use Phalcon\Forms\Element\Text;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\StringLength;

$name = new Text(
    "name"
);

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

$name->addValidator(
    new StringLength(
        [
            "min"            => 10,
            "messageMinimum" => "The name is too short",
        ]
    )
);

$form->add($name);

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Phalcon Interview Questions

    Explain Dependency Injection in Phalcon?

    Explain Dependency Injection in Phalcon?..

    Phalcon Interview Questions

    List basic features provided by Phalcon?

    List basic features provided by Phalcon?..