jhones/easy-validator

A simple validate data using PHP


Keywords
validator, easy
Licenses
MIT/BSD-3-Clause

Documentation

easy-validator

The Easy\Validator component rovides a simple way to validate the data using zend\validator. Error messages can be translated into the language of your country.

How to Install

composer require jhones/easy-validator

How to use

Step One

Import Easy\Validator into your application

use Easy\Validator\Validator;

Step Two

Create an instance of the object passing in the constructor the data to be validated

$validator = new Validator([
    'id' => 12334,
    'name' => 'Easy Validator',    
    'date' => '1990-06-06',
    'tel' => '3333-2212',
    'age' => ''
]);

Step Three

Call validate() method

$validator->validate();

Step Four

To check for validation errors that have occurred, just call the getErrors () method. Note that the method returns all errors that occurred in the validators: NotEmptyValidator, IntValidator, FloatValidator, DateValidator, AlnumValidator, AlphaValidator. Para cada validador é retornado todos os dados informados para validação

$errors = $validator->getErrors();
print_r($errors);

Example of Returned Errors

Array
(
    [errors] => Array
        (
            [not_empty] => Array
                (
                    [age] => Array
                        (
                            [is_valid] => false
                            [messages] => Array
                                (
                                    [is_empty] => Value is required and can't be empty
                                )

                        )

                )

            [integer] => Array
                (
                    [name] => Array
                        (
                            [is_valid] => false
                            [messages] => Array
                                (
                                    [not_int] => The input does not appear to be an integer
                                )

                        )
                    [date] => Array
                        (
                            [is_valid] => false
                            [messages] => Array
                                (
                                    [not_int] => The input does not appear to be an integer
                                )

                        )
                )
        )
)

Get Valids

It is also possible to return data that has been successfully validated by calling the getValids()

$valids = $validator->getErrors();
print_r($errors);

Example of Data Validated Successfully

 Array
 (
     [valids] => Array
         (
             [not_empty] => Array
                 (
                     [id] => Array
                         (
                             [is_valid] => true
                         )
 
                     [name] => Array
                         (
                             [is_valid] => true
                         )
 
                     [date] => Array
                         (
                             [is_valid] => true
                         )
 
                     [tel] => Array
                         (
                             [is_valid] => true
                         )
 
                 )
 
             [integer] => Array
                 (
                     [id] => Array
                         (
                             [is_valid] => true
                         )
 
                 ) 
         )
 )

Set Default Locale

Set default locale for show messages validator on your country language on config/config.php file

 'default_locale' => 'en'

By default the locale is 'en'. To change, just enter the location of your country. The possible locations are:

 'ar', 'bg', 'ca', 'cs', 'de', 'en', 'es', 'fi', 'fr', 'hr', 'hu', 'id',
 'it', 'ja', 'nl', 'no', 'pl', 'pt_BR', 'ru', 'se', 'sk', 'sl', 'sr', 'tr',
 'uk', 'zh', 'zh_TW'