PHP code example of hnrazevedo / validator

1. Go to this page and download the library: Download hnrazevedo/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/ */

    

hnrazevedo / validator example snippets


$valid = Validator::lang('pt_br')->namespace('App\\Rules')->execute($data);

namespace App\Rules;

use HnrAzevedo\Validator\Validator;
use HnrAzevedo\Validator\Rules;

Class User{

    public function __construct()
    {
        Validator::add($this, function(Rules $rules){
            $rules->action('login')
                  /*
                   * @property string $inputName
                   * @property array $rules
                   * @property string $textPlaceholder
                  */
                  ->field('email',['minlength'=>1,'filter'=>FILTER_VALIDATE_EMAIL,'les;
        });
        return $this;
    }

}

$data = [
    'email'=> '[email protected]',
    'password' => 123456,
    'password2' => 123456,
    'phones' => [
        '949164770','949164771','949164772'
    ],
    'birth' => '28/09/1996' 
    '_PROVIDER' => 'user',   /* Class responsible for validations */
    '_ROLE' => 'login'       /* Form action */
];

$valid = Validator::namespace('App\\Rules')->execute($data);

if(!$valid){
    $errors = [];
    foreach(Validator::getErrors() as $err => $message){
        $errors[] = [
            'input' => array_keys($message)[0],                 // Return name input error
            'message' => $message[array_keys($message)[0]]      // Return message error
        ];
    }
}

$json = Validator::namespace('App\\Rules')->toJson($data);

/**
 * Result:
 * {
 *     email:{als:"password"},
 *     remember:{

/*
 * @property string $namespace
 * @property array $data
 * @property string $language
 */
$serverRequest = $serverRequest->withAttribute('validator',[
    'namespace' => 'HnrAzevedo\\Validator\\Example\\Rules',
    'data' => $data,
    'lang' => 'pt_br'
]);