Validation Component


    Phalcon\Validation is an independent validation component that validates an arbitrary set of data. This component can be used to implement validation rules on data objects that do not belong to a model or collection.

    The following example shows its basic usage:

    The loosely-coupled design of this component allows you to create your own validators along with the ones provided by the framework.

    Initializing Validation

    Validation chains can be initialized in a direct manner by just adding validators to the Phalcon\Validation object. You can put your validations in a separate file for better re-use code and organization:

    1. use Phalcon\Validation;
    2. use Phalcon\Validation\Validator\Email;
    3. use Phalcon\Validation\Validator\PresenceOf;
    4. class MyValidation extends Validation
    5. {
    6. public function initialize()
    7. {
    8. $this->add(
    9. 'name',
    10. new PresenceOf(
    11. [
    12. 'message' => 'The name is required',
    13. ]
    14. )
    15. );
    16. $this->add(
    17. 'email',
    18. new PresenceOf(
    19. [
    20. 'message' => 'The e-mail is required',
    21. ]
    22. )
    23. );
    24. $this->add(
    25. 'email',
    26. new Email(
    27. [
    28. 'message' => 'The e-mail is not valid',
    29. ]
    30. )
    31. );
    32. }
    33. }

    Then initialize and use your own validator:

    1. <?php
    2. $validation = new MyValidation();
    3. $messages = $validation->validate($_POST);
    4. if (count($messages)) {
    5. foreach ($messages as $message) {
    6. echo $message, '<br>';
    7. }
    8. }

    Validators

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

    1. <?php
    2. use Phalcon\Validation;
    3. use Phalcon\Validation\Message;
    4. use Phalcon\Validation\Validator;
    5. class IpValidator extends Validator
    6. {
    7. /**
    8. * Executes the validation
    9. *
    10. * @param Validation $validator
    11. * @param string $attribute
    12. * @return boolean
    13. */
    14. public function validate(Validation $validator, $attribute)
    15. {
    16. $value = $validator->getValue($attribute);
    17. if (!filter_var($value, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6)) {
    18. $message = $this->getOption('message');
    19. if (!$message) {
    20. $message = 'The IP is not valid';
    21. }
    22. $validator->appendMessage(
    23. new Message($message, $attribute, 'Ip')
    24. return false;
    25. }
    26. return true;
    27. }
    28. }

    It is important that validators return a valid boolean value indicating if the validation was successful or not.

    By using Phalcon\Validation\Validator\Callback you can execute custom function which must return boolean or new validator class which will be used to validate the same field. By returning true validation will be successful, returning false will mean validation failed. When executing this validator Phalcon will pass data depending what it is - if it’s an entity (i.e. a model, a stdClass etc.) then entity will be passed, otherwise data (i.e an array like $_POST). There is example:

    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 . The set of messages generated can be retrieved with the getMessages() method. Each message provides extended information like the attribute that generated the message or the message type:

    1. $messages = $validation->validate();
    2. if (count($messages)) {
    3. foreach ($messages as $message) {
    4. echo 'Message: ', $message->getMessage(), "\n";
    5. echo 'Field: ', $message->getField(), "\n";
    6. echo 'Type: ', $message->getType(), "\n";
    7. }
    8. }

    You can pass a message parameter to change/translate the default message in each validator, even it’s possible to use the wildcard :field in the message to be replaced by the label of the field:

    1. <?php
    2. use Phalcon\Validation\Validator\Email;
    3. $validation->add(
    4. 'email',
    5. new Email(
    6. [
    7. 'message' => 'The e-mail is not valid',
    8. ]
    9. )
    10. );

    By default, the getMessages() method returns all the messages generated during validation. You can filter messages for a specific field using the filter() method:

    1. <?php
    2. $messages = $validation->validate();
    3. if (count($messages)) {
    4. // Filter only the messages generated for the field 'name'
    5. $filteredMessages = $messages->filter('name');
    6. foreach ($filteredMessages as $message) {
    7. echo $message;
    8. }
    9. }

    Filtering of Data

    Filtering and sanitizing is performed using the component. You can add more filters to this component or use the built-in ones.

    When validations are organized in classes, you can implement the beforeValidation() and afterValidation() methods to perform additional checks, filters, clean-up, etc. If the beforeValidation() method returns false the validation is automatically cancelled:

    1. <?php
    2. use Phalcon\Validation;
    3. class LoginValidation extends Validation
    4. {
    5. public function initialize()
    6. {
    7. // ...
    8. }
    9. /**
    10. * Executed before validation
    11. *
    12. * @param array $data
    13. * @param object $entity
    14. * @param Phalcon\Validation\Message\Group $messages
    15. * @return bool
    16. */
    17. public function beforeValidation($data, $entity, $messages)
    18. {
    19. if ($this->request->getHttpHost() !== 'admin.mydomain.com') {
    20. $messages->appendMessage(
    21. new Message('Only users can log on in the administration domain')
    22. );
    23. return false;
    24. }
    25. return true;
    26. }
    27. /**
    28. * Executed after validation
    29. *
    30. * @param array $data
    31. * @param object $entity
    32. * @param Phalcon\Validation\Message\Group $messages
    33. */
    34. public function afterValidation($data, $entity, $messages)
    35. {
    36. // ... Add additional messages or perform more validations
    37. }
    38. }

    Cancelling Validations

    By default all validators assigned to a field are tested regardless if one of them have failed or not. You can change this behavior by telling the validation component which validator may stop the validation:

    1. <?php
    2. use Phalcon\Validation;
    3. use Phalcon\Validation\Validator\Regex;
    4. $validation = new Validation();
    5. $validation->add(
    6. 'telephone',
    7. new PresenceOf(
    8. [
    9. 'message' => 'The telephone is required',
    10. 'cancelOnFail' => true,
    11. ]
    12. )
    13. );
    14. $validation->add(
    15. 'telephone',
    16. new Regex(
    17. [
    18. 'message' => 'The telephone is required',
    19. 'pattern' => '/\+44 [0-9]+/',
    20. ]
    21. )
    22. );
    23. $validation->add(
    24. 'telephone',
    25. new StringLength(
    26. [
    27. 'messageMinimum' => 'The telephone is too short',
    28. 'min' => 2,
    29. ]
    30. )
    31. );

    The first validator has the option cancelOnFail with a value of true, therefore if that validator fails the remaining validators in the chain are not executed.

    If you are creating custom validators you can dynamically stop the validation chain by setting the cancelOnFail option:

    1. <?php
    2. use Phalcon\Validation;
    3. use Phalcon\Validation\Message;
    4. use Phalcon\Validation\Validator;
    5. class MyValidator extends Validator
    6. {
    7. /**
    8. * Executes the validation
    9. *
    10. * @param Phalcon\Validation $validator
    11. * @param string $attribute
    12. * @return boolean
    13. */
    14. public function validate(Validation $validator, $attribute)
    15. {
    16. // If the attribute value is name we must stop the chain
    17. if ($attribute === 'name') {
    18. $validator->setOption('cancelOnFail', true);
    19. }
    20. // ...
    21. }
    22. }

    Avoid validating empty values

    You can pass the option allowEmpty to all the built-in validators to avoid the validation to be performed if an empty value is passed:

    1. <?php
    2. use Phalcon\Validation;
    3. class CompanyValidation extends Validation
    4. {
    5. /**
    6. * @var PhoneValidation
    7. */
    8. protected $phoneValidation;
    9. public function initialize()
    10. {
    11. $this->phoneValidation = new PhoneValidation();
    12. }
    13. public function afterValidation($data, $entity, $messages)
    14. {
    15. $phoneValidationMessages = $this->phoneValidation->validate(
    16. $data['phone']
    17. );
    18. $messages->appendMessages(
    19. $phoneValidationMessages
    20. );
    21. }