2017 © Pedro Peláez
 

library validation

Simple Extensible Validation Library for PHP.

image

polycademy/validation

Simple Extensible Validation Library for PHP.

  • Friday, November 8, 2013
  • by CMCDragonkai
  • Repository
  • 2 Watchers
  • 0 Stars
  • 31 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 16 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Validation Library

A simple, extensible validation library for PHP with support for filtering and validating any input array. Provides a more concise interface than the upstream fork., (*1)

0.0 Table of Contents

  • Introduction
  • Examples
  • Rule Reference
  • Installation

1.0 Introduction

This library provides a simple way to validate an input array against a set of rules. Input could come from $_POST or any other data source., (*2)

Each field can have its own label, pre-filters and rules applied to it. Rules extend a very simple interface, making adding custom rules very easy. The Validator object itself can be executed multiple times against different datasets, making it very useful for processing dynamic data., (*3)

You can easily extend this validator by simply adding more rules by extending "\Polycademy\Validation\Rule". Make sure to check out the source code!, (*4)

2.0 Examples

You can add rules and filters individually!, (*5)

use Polycademy\Validation\Validator;
use Polycademy\Validation\Rule;

$validator = new Validator();
$validator
    ->set_label('name', 'first name')
    ->set_label('email', 'email address')
    ->set_label('password2', 'password confirmation')
    ->add_filter('name', 'trim')
    ->add_filter('email', 'trim')
    ->add_filter('email', 'strtolower')
    ->add_rule('name', new Rule\MinLength(5))
    ->add_rule('name', new Rule\MaxLength(10))
    ->add_rule('email', new Rule\MinLength(5))
    ->add_rule('email', new Rule\Email())
    ->add_rule('password', new Rule\Matches('password2'))
;

if($validator->is_valid($_POST)) {
    print_r($validator->get_data());
} else {
    print_r($validator->get_errors());
}

Or you can use the setup_rules and fluently add all the rules., (*6)

use Polycademy\Validation\Validator;
use Polycademy\Validation\Rule;

$validator = new Validator();

$validator->setup_rules(array(
    'name' => array(
        'set_label:Course Name',
        'NotEmpty',
        'AlphaNumericSpace',
        'MinLength:5',
        'MaxLength:50',
    ),
    'starting_date' => array(
        'set_label:Starting Date',
        'Regex:/^(19|20)\d\d\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$/',
    ),
    'days_duration' => array(
        'set_label:Course Duration',
        'NotEmpty',
        'Number',
        'NumRange:1,200',
    ),
    'times' => array(
        'set_label:Course Times',
        'NotEmpty',
        'MinLength:1',
        'MaxLength:100',
        'AlphaNumericSpace',
    ),
    'number_of_applications' => array(
        'set_label:Number of Applicants',
        'Number',
        'NumRange:0,100',
    ),
    'number_of_students' => array(
        'set_label:Number of Students',
        'Number',
        'NumRange:0,100',
    ),
));

if(!$validator->is_valid($data)){

    //returns array of key for data and value
    $errors = $validator->get_errors();

}

More detailed examples can be found in ./examples., (*7)

3.0 Rule Reference

  • NotEmpty Makes this a required field
  • Equal Input must match provided string
  • NotEqual Input must not match provided string
  • Matches Input must match value of another field
  • InArray Input must be in an array of values
  • MinLength Input length must be greater than or equal to value
  • MaxLength Input length must be less than or equal to value
  • ExactLength Input length must be exactly value
  • Alpha Input can only contain a-z characters
  • AlphaSpace Input can only contain a-z characters and whitespace
  • AlphaNumeric Input can contain a-z and 0-9
  • AlphaNumericSpace Input can contain a-z and 0-9 and whitespace
  • AlphaSlug Input can contain a-z, 0-9, - and _
  • Regex Input must match provided regular expression
  • Email Input must be a valid email format
  • URL Input must be a valid URL format
  • IP Input must be a valid IPv4 or v6 address
  • True Input must be true e.g. checkbox
  • Number Input must be numeric e.g. -99 or 123.45
  • NumNatural Input must be an integer zero or above
  • NumMin Input value must be greater than or equal to value
  • NumMax Input value must be less than or equal to value
  • NumRange Input value must be between min and max values

4.0 Installation

You can install by using Composer. Simply add this to your composer.json, (*8)

"polycademy/validation": "*"

The Versions

08/11 2013

dev-master

9999999-dev https://github.com/Polycademy/PHP-Validation

Simple Extensible Validation Library for PHP.

  Sources   Download

WTFPL

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side

08/11 2013

v1.0.9

1.0.9.0 https://github.com/Polycademy/PHP-Validation

Simple Extensible Validation Library for PHP.

  Sources   Download

WTFPL

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side

16/03 2013

v1.0.8

1.0.8.0 https://github.com/Polycademy/PHP-Validation

Simple Extensible Validation Library for PHP.

  Sources   Download

WTFPL

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side

16/03 2013

v1.0.7

1.0.7.0 https://github.com/Polycademy/PHP-Validation

Simple Extensible Validation Library for PHP.

  Sources   Download

WTFPL

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side

16/03 2013

v1.0.6

1.0.6.0 https://github.com/Polycademy/PHP-Validation

Simple Extensible Validation Library for PHP.

  Sources   Download

WTFPL

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side

16/03 2013

v1.0.5

1.0.5.0 https://github.com/Polycademy/PHP-Validation

Simple Extensible Validation Library for PHP.

  Sources   Download

WTFPL

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side

16/03 2013

v1.0.4

1.0.4.0 https://github.com/Polycademy/PHP-Validation

Simple Extensible Validation Library for PHP.

  Sources   Download

WTFPL

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side

16/03 2013

v1.0.3

1.0.3.0 https://github.com/Polycademy/PHP-Validation

Simple Extensible Validation Library for PHP.

  Sources   Download

WTFPL

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side

12/01 2013

v1.0.2

1.0.2.0 https://github.com/Dachande663/PHP-Validation

A simple, extensible validation library for PHP with support for filtering and validating any input array along with generating client side validation code.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side

12/01 2013

v1.0.1

1.0.1.0 https://github.com/Dachande663/PHP-Validation

A simple, extensible validation library for PHP with support for filtering and validating any input array along with generating client side validation code.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side

05/01 2013

v1.0.0

1.0.0.0

Validation libraries for forms and services

  Sources   Download

The Requires

  • php >=5.3.0

 

services jquery validation models forms rules client-side