PHP code example of bonuscred / data-validator

1. Go to this page and download the library: Download bonuscred/data-validator library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

bonuscred / data-validator example snippets


$data = [
    'field_cnpj'   => '81.287.353/0001-16',
    'field_cpf'    => '124.784.370-07',
    'field_int'    => 123,
    'field_string' => 'minhastring',
    'field_date'   => '2024-01-01 00:00:00'
];

$validator = new DataValidator\Validator;
$validator->cnpj('field_cnpj');
$validator->cpf('field_cpf');
$validator->integer('field_int');
$validator->string('field_string');
$validator->date('field_date', 'Y-m-d H:i:s');

$validator->validate($data); //true

DataValidator\Field\Region\Cpf::isValid('124.784.370-07'); //true
DataValidator\Field\Array\Enum::isValid('a', ['b', 'c']); //false

//Evita disparo de Exception
DataValidator\Error::throw(false);

$data = [
    'field_cpf'    => 'not-a-cpf'
    'field_date'   => 'not-a-date'
];

$validator = new DataValidator\Validator;
$validator->cpf('field_cpf');
$validator->date('field_date');

if(!$validator->validate($data)){
    $errors = $validator->getErrors(); //Array contendo todos os erros de validação

    echo $validator->getLastError()->field; //field_date
    echo ': ';
    echo $validator->getLastError()->message; //Mensagem de data inválida
}

(new DataValidator\Validator)->cnpj('field_cnpj')
                             ->cpf('field_cpf')
                             ->integer('field_int')
                             ->string('field_string')
                             ->date('field_date', 'Y-m-d H:i:s')
                             ->validate($data);

$data = [
    'field_int' => 123,
    'complex'   => [
        'a' => 'not-empty',
        'b' => [
            'ba' => 1,
            'bb' => 'is-string',
        ],
        'c' => [
            [
                'ca' => 1,
                'cb' => 'cb_a'
            ],
            [
                'ca' => 2,
                'cb' => 'cb_b'
            ]
        ]
    ]
];


$validator = new DataValidator\Validator;
$validator->integer('field_int')
          ->multidimensional('complex', [
            'a' => new DataValidator\Field\Calculation\NotEmpty,
            'b' => [
                'ba' => new DataValidator\Field\Type\Integer,
                'bb' => new DataValidator\Field\Type\Stringval                
            ],
            'c' => new DataValidator\Field\Array\Multidimensional([
                'ca' => new DataValidator\Field\Type\Integer,
                'cb' => new DataValidator\Field\Array\Enum(['cb_a', 'cb_b'])
            ], true)
          ]);

$validator->validate($data); //true

$validator->integer('field_int')
          ->addConditional(
            'field_int',
            new DataValidator\Field\Calculation\Equal(123), 
            'field_condicional', 
            new DataValidator\Field\Calculation\NotEmpty
         );

$validator->validate(['field_int' => 321]); //true
$validator->validate(['field_int' => 123]); //false

$validator->integer('field_int')->validate(['field_int' => 123]); //true
$validator->not_integer('field_int')->validate(['field_int' => 123]); //false

DataValidator\Lang\Translator::set('en');