2017 © Pedro PelĂĄez
 

library validator

Validation library

image

crodas/validator

Validation library

  • Friday, March 25, 2016
  • by crodas
  • Repository
  • 1 Watchers
  • 2 Stars
  • 2,121 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 15 Versions
  • 0 % Grown

The README.md

Validator Build Status

Generate static validators to validate your data in PHP. Static means that the rules are defined offline, then the library generates code to run the validations. We don't run any checking at runtime., (*1)

If you need to define validation rules on the fly, for instance you have a CMS or something like that, then this library would be of little help (unless you call the crodas\Validator\Builder manually), in that case I would recommend to take a look at @Respect's validation library which has been an inspiration for this library., (*2)

## How to installed

The easiest way of install the package is using Composer:, (*3)

composer "crodas/validator":\*

How to use it

Our object

All the validation rules are defined used Annotatios, it should be like this:, (*4)

/** @Validate */
class User
{
    /** @Between([18, 99], "Invalid age range, it should be between 18-99") */ 
    protected $age;

    /** 
     * @NoWhitespace("Spaces are not allowed")
     * @MinLength([5], "{$value} is too short") 
     * @MaxLength([10], "{$value} is too long")
     */
    public $username;
}

Easy way

You can just call the crodas/Validator/validate function, it will take care of the everything else., (*5)

$user = new User;
$user->age = 17;
$user->username = "invalid username";
if (!crodas\Validator\validate($user, $errors)) {
    echo "<h1>There has been an error</h1>";
    foreach ($errors as $property => $error) {
        echo "...\n";
    }
    exit;
}

Hard way

You can also generate the validation object reading the PHP files from a given directory. This project exposes it's engine and compile so this can be done very easily. The first thing you have to do is create the Validator object., (*6)

require "vendor/autoload.php";

$val = new crodas\Validator\Init("/classes/", "/tmp/foo.php");

The validator object would look for classes defined inside /classes/ and its subdirectories the first time. It would then load classes with @Validate annotation in it., (*7)

To validate an object you would have to do something like this., (*8)

$errors = $val->validate(new User);
if (!empty($errors)) {
  foreach ($errors as $field => $exception) {
     echo "{$field} is not valid ( {$exception} )\n";
  }
}

The Versions

25/03 2016

dev-develop

dev-develop

Validation library

  Sources   Download

BSD-4-Clause

The Requires

 

The Development Requires

by César D. Rodas

25/03 2016

dev-master

9999999-dev

Validation library

  Sources   Download

BSD-4-Clause

The Requires

 

The Development Requires

by César D. Rodas

25/03 2016

v0.2.2

0.2.2.0

Validation library

  Sources   Download

BSD-4-Clause

The Requires

 

The Development Requires

by César D. Rodas

29/12 2015
29/12 2015
23/12 2015
17/06 2015
28/01 2015
03/01 2015
05/09 2014
05/09 2014
12/05 2014
06/04 2014

v0.1.1

0.1.1.0

Validation library

  Sources   Download

BSD-4-Clause

The Requires

 

by César D. Rodas

25/10 2013

v0.1.0

0.1.0.0

Validation library

  Sources   Download

BSD-4-Clause

The Requires

 

by César D. Rodas