2017 © Pedro Peláez
 

library validator

Data validator library for PHP

image

fabstract/validator

Data validator library for PHP

  • Friday, June 8, 2018
  • by fabsolute
  • Repository
  • 2 Watchers
  • 0 Stars
  • 244 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 1 Forks
  • 1 Open issues
  • 15 Versions
  • 92 % Grown

The README.md

, (*1)

Build Status Total Downloads Latest Stable Version License , (*2)

Validator

This library introduces a small framework to validate certain variables against some rules. This library is especially useful to validate client POST or form data in the backend side., (*3)

This library is fully compatible with any framework and library. You can feel free to use it with Symfony, Laravel, or whichever framework you are using with nothing to fear for., (*4)

What Does It Do?

You can use this library to check if given value is valid string, array, integer, float, whether is belongs to certain set of values, objects and so on. In case validation fails, library generates detailed messages as to why., (*5)

Installation

Note: PHP 7.1 or higher is required., (*6)

  1. Install composer.
  2. Run composer require fabstract/validator.

How to use

First you need to create a class that implements ValidatableInterface and set your validations. Let's call it validatable. Then you need an instance of Validator. Finally, set your validatable object's fields and validate it via validator. Let's check below, we will use pre-defined validation classes (for instance, StringValidation class) for this example:, (*7)

use Fabstract\Component\Validator\ValidatableInterface;
use Fabstract\Component\Validator\Validation\PatternValidation;
use Fabstract\Component\Validator\Validation\StringValidation;
use Fabstract\Component\Validator\ValidationMetadata;
use Fabstract\Component\Validator\Validator;

// Create class
class RegistrationPostData implements ValidatableInterface
{

    // Has to be 3-30 characters
    public $name = null;
    // Has to include at least a letter and number, be between 8-32 characters
    // so, it needs to match "^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,32}$"
    public $password = null;

    /**
     * @param ValidationMetadata $validation_metadata
     * @return void
     */
    public function configureValidationMetadata($validation_metadata)
    {
        $validation_metadata
            ->addValidation('name', StringValidation::create()
                ->setMinLength(3)
                ->setMaxLength(30))
            ->addValidation('password', PatternValidation::create('/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,32}$/'));
    }
}

// Create validator instance, one instance is enough
$validator = new Validator();

// Let's create instance of our RegistrationPostData and fill it with POST data
$post_data = new RegistrationPostData();
$post_data->name = $_POST['name'];
$post_data->password = $_POST['password'];

// Validate the data
$validation_error_list = $validator->validate($post_data);

// Here if validation error list is empty, it means all your post data is valid.
$is_post_data_valid = count($validation_error_list) === 0;
if (!$is_post_data_valid) {
    // If not empty, you can see meaningful validation messages like below
    foreach ($validation_error_list as $validation_error) {
        echo $validation_error;
        echo PHP_EOL;
    }
}

Suppose you sent a data like below:, (*8)

curl -X POST localhost -F'name=ac' -F'password=123'

You will see a result like below:, (*9)

Validation failed at "name". Message is "String must be at least 3 character(s) long.".
Validation failed at "password". Message is "Value must match pattern /^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,32}$/.".

The Versions

08/06 2018

dev-master

9999999-dev

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

08/06 2018

v0.4.10

0.4.10.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

06/06 2018

v0.4.9

0.4.9.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

03/06 2018

v0.4.8

0.4.8.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

01/06 2018

v0.4.7

0.4.7.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

30/05 2018

v0.4.6

0.4.6.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

17/05 2018

v0.4.5

0.4.5.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

14/05 2018

v0.4.4

0.4.4.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

11/05 2018

v0.4.3

0.4.3.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

06/05 2018

v0.4.2

0.4.2.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

30/04 2018

v0.4.1

0.4.1.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

30/04 2018

v0.4.0

0.4.0.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

19/04 2018

v0.3.0

0.3.0.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

29/03 2018

v0.2.0

0.2.0.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator

29/03 2018

v0.1.0

0.1.0.0

Data validator library for PHP

  Sources   Download

MIT

The Requires

 

php validator