Validation ========== Phalcon\\Validation 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: .. code-block:: php add('name', new PresenceOf(array( 'message' => 'The name is required' ))); $validation->add('email', new PresenceOf(array( 'message' => 'The e-mail is required' ))); $validation->add('email', new Email(array( 'message' => 'The e-mail is not valid' ))); $messages = $validation->validate($_POST); if (count($messages)) { foreach ($messages as $message) { echo $message, '
'; } } The loose-coupled design of this component allows you to create your own validators together with the ones provided by the framework. Initializing validation ----------------------- As seen before, Validation chains can be initialized in a direct manner by just adding validators to the Phalcon\\Validation object. You can re-use code or organize better your validations implementing them in a separated file: .. code-block:: php add('name', new PresenceOf(array( 'message' => 'The name is required' ))); $this->add('email', new PresenceOf(array( 'message' => 'The e-mail is required' ))); $this->add('email', new Email(array( 'message' => 'The e-mail is not valid' ))); } } .. code-block:: php validate($_POST); if (count($messages)) { foreach ($messages as $message) { echo $message, '
'; } } 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. | :doc:`Example <../api/Phalcon_Validation_Validator_PresenceOf>` | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+ | Identical | Validates that a field's value is the same as a specified value | :doc:`Example <../api/Phalcon_Validation_Validator_Identical>` | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+ | Email | Validates that field contains a valid email format | :doc:`Example <../api/Phalcon_Validation_Validator_Email>` | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+ | ExclusionIn | Validates that a value is not within a list of possible values | :doc:`Example <../api/Phalcon_Validation_Validator_ExclusionIn>` | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+ | InclusionIn | Validates that a value is within a list of possible values | :doc:`Example <../api/Phalcon_Validation_Validator_InclusionIn>` | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+ | Regex | Validates that the value of a field matches a regular expression | :doc:`Example <../api/Phalcon_Validation_Validator_Regex>` | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+ | StringLength | Validates the length of a string | :doc:`Example <../api/Phalcon_Validation_Validator_StringLength>` | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+ | Between | Validates that a value is between two values | :doc:`Example <../api/Phalcon_Validation_Validator_Between>` | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+ | Confirmation | Validates that a value be the same as as other present in the data | :doc:`Example <../api/Phalcon_Validation_Validator_Confirmation>` | +--------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------+-------------------------------------------------------------------+ Additional validators can be created by the developer. The following example explains how to create a validator for this component: .. code-block:: php 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; } } Is important that validators return a valid boolean value indicating if the validation was successful or not. Validation Messages ------------------- :doc:`Phalcon\\Validation <../api/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 :doc:`Phalcon\\Validation\\Message <../api/Phalcon_Mvc_Model_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: .. code-block:: php 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: .. code-block:: php 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: .. code-block:: php add('email', new Email(array( 'message' => 'The e-mail is not valid' ))); By default, 'getMessages' returns all the messages generated in the validation, you can filter messages for a specific field using 'filter': .. code-block:: php validate(); if (count($messages)) { //Filter only the messages generated for the field 'name' foreach ($validation->getMessages()->filter('name') as $message) { echo $message; } } Filtering of Data ----------------- Data can be filtering prior to the validation ensuring that malicious data or wrong is not going to be validated as a proper one. .. code-block:: php add('name', new PresenceOf(array( 'message' => 'The name is required' ))) ->add('email', new PresenceOf(array( 'message' => 'The email is required' ))); //Filter any extra space $validation->setFilters('name', 'trim'); $validation->setFilters('email', 'trim'); Filtering/Sanitizing is performed using the :doc:`filter `: component. You can add more filters to this component or use the built-in ones. Validation Events ----------------- When validations are organized in classes, you can implement the methods 'beforeValidation' and 'afterValidation' to perform additional checks/clean-up etc. If 'beforeValidation' returns 'false' the validation is automatically cancelled: .. code-block:: php request->getHttpHost() != 'admin.mydomain.com') { $messages->appendMessage(new Message('Users only can log on in the administration domain')); return false; } return true; } /** * Executed after validation * * @param array $data * @param object $entity * @param Phalcon\Validation\Message\Group $messages */ public function afterValidation($data, $entity, $messages) { //... add additional messages or perform more validations } } Validation Cancelling --------------------- By default, all validators assigned to a field are validated regardless if one of them have failed or not. You can change this behavior by telling the validation component which validator must stop the validation: .. code-block:: php add('telephone', new PresenceOf(array( 'message' => 'The telephone is required', 'cancelOnFail' => true ))) ->add('telephone', new Regex(array( 'message' => 'The telephone is required', 'pattern' => '/\+44 [0-9]+/' ))) ->add('telephone', new StringLength(array( 'minimumMessage' => 'The telephone is too short', 'min' => 2 ))); The first validator has the option 'cancelOnFail' => true, therefore if that validator fails the next validator in the chain is not executed. If you're creating custom validators, you can dynamically stop the validation chain, by setting the 'cancelOnFail' option: .. code-block:: php setOption('cancelOnFail', true); } //... } }