Is there any library or PHP plugin for field validation?

1

Is there a library, plugin or something that makes it easier and more practical to validate fields, string and etc?

Ex: I have to get the username, and as it is a name will only accept letters (and spaces), something like:

$validateFields = new validateFields;
$userName = $validateFields->validateName($_POST['name']);
$userPass = ....
$userPhone = .....
    
asked by anonymous 14.10.2016 / 07:21

1 answer

3

Respect Validation

  

The most awesome validation engine ever created for PHP

Translating:

  

The most impressive validation engine ever built for PHP

I really agree. The guys thought about everything when they made this library. This library has validation for many data types: cpf, cnh, cnpj, numeric, vowels, maximum, minimum, non-empty, etc.

If I'm not mistaken, they have a great contribution from Brazilian programmers.

Here's how to install (you need Composer):

composer require respect/validation

Repository link in Github

Here you have several examples of how to use.

I'll show you an example from the documentation itself:

use Respect\Validation\Validator as v;

$user = new stdClass;
$user->name = 'Alexandre';
$user->birthdate = '1987-07-01';

$userValidator = v::attribute('name', v::stringType()->length(1,32))
                  ->attribute('birthdate', v::date()->age(18));

$userValidator->validate($user); // bool(true)
    
14.10.2016 / 14:32