Is there a PHP library or plugin for field validation?

Asked

Viewed 330 times

1

Is there a library, plugin or something that makes the task of validating fields, string and etc easier and more practical?

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

$validateFields = new validateFields;
$userName = $validateFields->validateName($_POST['name']);
$userPass = ....
$userPhone = .....

1 answer

4


Respect Validation

The Most awesome validation engine Ever created for PHP

Translating:

The most impressive validation engine ever created for PHP

I really agree. The guys thought of everything when they built this library. This library has validation for numerous types of data: 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

Link to repository on Github

Here you have several examples of how to use.

Let me 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)
  • 1

    Oops, thank you very much, I’ll give it a try

Browser other questions tagged

You are not signed in. Login or sign up in order to post.