2017 © Pedro Peláez
 

library validatr

Simple, multilingual, standalone PHP Validator class - Validatr.

image

boomerang/validatr

Simple, multilingual, standalone PHP Validator class - Validatr.

  • Wednesday, April 6, 2016
  • by boomerang
  • Repository
  • 1 Watchers
  • 8 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 11 Versions
  • 0 % Grown

The README.md

Validatr

Build Status, (*1)

Validatr is a simple multilingual PHP Validation library for checking user's data., (*2)

  • Simple form for rules and errors messages
  • Flexible and extendable library with your callback rules
  • Enjoyment!

Validatr is a standalone PHP class, which can be extended for your needs., (*3)

Table of contents

Getting started

  1. PHP >= 5.4 is required
  2. Install Validatr convenient for you
  3. Check your data with Validatrs rules
  4. Enjoy!

Installation

1) Via composer, (*4)

{
    "require": {
        "boomerang/validatr": "~3.0"
    }
}

To download the library run the command:, (*5)

$ php composer.phar require boomerang/validatr

or, (*6)

$ composer require boomerang/validatr

2) Git clone, (*7)

git clone git@github.com:booomerang/Validatr.git

3) Download, (*8)

Download .zip, (*9)

Unzip it and copy the directory into your PHP project directory., (*10)

How to use

Simple Form, (*11)




Form Handler with Validatr class, (*12)

// Set rules for some fields in form
$rules = [
    'name' => 'required|min:3|max:15|equal:Awesome:[s]',
    'password' => 'required|min:3|custom_equal:OK', // Custom rule, defined below
    'email' => 'required|email',
    'checkbox' => 'bool'
];

// Your error messages on your language
$messages = array (
    'name' => array (
        'required' => 'Поле обязательно для заполнения',
        'min' => 'Minimum required 3 characters',
        'max' => 'Maximal zulässige fünfzehn Zeichen',
        'equal' => 'Значение должно равняться - Awesome'
    ),
    'password' => array (
        'required' => 'Поле обязательно для заполнения',
        'min' => 'Minimalement acceptables 3 caractères',
        'custom_equal' => 'Кастомное сообщение для правила custom_equal'
    ),
    'checkbox' => array (
        'bool' => 'Вы не выставили галочку!'
    )
);

$validator = new \Validatr\Validator();

// Adding custom rule
$validator->addRule('custom_equal', function($dataValue, $ruleValue){
    return ($dataValue == $ruleValue) ? true : false;
});

$result = $validator->validate($_POST, $rules, $messages);

echo "

";
print_r($result);
echo "
";

Set messages

//Todo

Return Values

Method validate() returns one of two types:, (*13)

AN ARRAY containing in keys names of form fields and in values nested associative array containing in keys validation rules and in values error messages. (See below example)., (*14)

or, (*15)

A BOOLEAN value of TRUE if the validation was successful., (*16)

Return values example:, (*17)

The result may be (if all fields were sent empty):, (*18)

Array
(
    [name] => Array
        (
            [required] => Поле обязательно для заполнения
            [min] => Minimum required 3 characters
            [equal] => Значение должно равняться - Boss
        )

    [password] => Array
        (
            [required] => Поле обязательно для заполнения
            [min] => Minimalement acceptables 3 caractères
        )

    [email] => Array
        (
            [required] => This field is required
            [email] => Invalid email address
        )

    [checkbox] => Array
        (
            [bool] => Вы не выставили галочку!
        )

)

Or if all fields was validated successfully:, (*19)

1

Available rules:

- required  // Checks if field's value is not empty
- min       // Checks if the number of characters of field's value not less than rule value (UTF-8)
- max       // Checks if the number of characters of field's value not greater than rule value (UTF-8)
- email     // Checks if field's value is a valid email adress
- numeric   // Checks if field contains only numeric value
- bool      // Checks if field's value is boolean
- alpha     // Checks if field's value contains only alphabetic characters (UTF-8)
- alnum     // Checks if field's value contains only alphabetic and numeric characters (UTF-8)
- alnumWith // Checks if field's value contains only alphabetic and numeric characters (UTF-8) and some else custom characters
- in        // Checks if value is included in the given list of values.
- notIn     // Checks if value is not included in the given list of values.
- equal     // checks if value is equal to rule value (Strict or not)
- notEqual  // checks if value is bot equal to rule value (Strict or not)

Creating your own validating rules

For creating your own validating rules use addRule method., (*20)

// Lets create our own 'equal' rule.

$data = array (
    'name' => 'ok'
);

$rules = array(
    'name' => 'custom_equal:OK:strict:upper'
);

$messages = array(
    'name' => array(
        'custom_equal' => 'The value must be equal "ok"'
    )
);

// The callback function may receives three arguments:
// 1st arg - field value - 'ok' (From $data)
// 2nd arg - rule value - 'OK' (From $rules 2nd param)
// 3rd arg - additional params - array (From $rules starting 3nd and more params)

// $params in this example is an array - array('strict', 'upper'); But not used
// It should return a boolean value indicating whether the value is valid.
$validator->addRule('custom_equal', function($dataValue, $ruleValue, $params){
    return ($dataValue == $ruleValue) ? true : false;
});

$result = $validator->validate($data, $rules, $messages);

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Make your changes
  4. Write the unit tests for your new feature (phpunit)
  5. Commit your changes (git commit -am 'Add some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create new Pull Request

Versioning

Validatr is maintained under the Semantic Versioning guidelines. Sometimes we screw up, but we'll adhere to these rules whenever possible., (*21)

Releases will be numbered with the following format:, (*22)

<major>.<minor>.<patch>

And constructed with the following guidelines:, (*23)

  • Breaking backward compatibility bumps the major while resetting minor and patch
  • New additions without breaking backward compatibility bumps the minor while resetting the patch
  • Bug fixes and misc changes bumps only the patch

For more information on SemVer, please visit http://semver.org/., (*24)

Brazenly copied from Bootstrap's README.md =), (*25)

Authors

Alex D., (*26)


Inspired by:, (*27)

  • http://jqueryvalidation.org/
  • https://github.com/selahattinunlu/phpValidator
  • https://github.com/Wixel/GUMP
  • http://laravel.com/docs/validation
  • https://github.com/Respect/Validation

Copyright 2016 Alex D, licensed under the MIT., (*28)

The Versions

06/04 2016

dev-master

9999999-dev https://github.com/booomerang

Simple, multilingual, standalone PHP Validator class - Validatr.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Duply

php validator validation validatr form validator

06/04 2016

v3.0.0-alpha

3.0.0.0-alpha https://github.com/booomerang

Simple, multilingual, standalone PHP Validator class - Validatr.

  Sources   Download

MIT

The Requires

  • php >=5.4.0

 

The Development Requires

by Alex Duply

php validator validation validatr form validator

26/03 2014

v2.0.0-beta

2.0.0.0-beta https://github.com/booomerang

Simple, multilingual, standalone PHP Validator library - Validatr.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Alex Duplii

php validator validation validatr form validator

27/01 2014

v1.0.1-rc

1.0.1.0-RC https://github.com/booomerang

Simple, multilingual, standalone PHP Validator library.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Alex Duplii

php validator validation validatr form validator

27/01 2014

v1.0.0-beta

1.0.0.0-beta https://github.com/booomerang

Simple, multilingual, standalone PHP Validator library.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Alex Duplii

php validator validation validatr form validator

23/01 2014

v0.1.5

0.1.5.0 https://github.com/booomerang

Simple Php standalone Validator class.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Alex Duplii

php validator validatr form validator

23/01 2014

v0.1.4

0.1.4.0 https://github.com/booomerang

Simple Php standalone Validator class.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Alex Duplii

php validator validatr form validator

21/01 2014

v0.1.3

0.1.3.0 https://github.com/booomerang

Simple Php standalone Validator class.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Alex Duplii

php validator validatr form validator

21/01 2014

v0.1.2

0.1.2.0 https://github.com/booomerang

Simple Php standalone Validator class.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Alex Duplii

php validator validatr form validator

20/01 2014

v0.1.1

0.1.1.0 https://github.com/booomerang

Simple Php standalone Validator class.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Alex Duplii

php validator validatr form validator

19/01 2014

v0.1.0

0.1.0.0 https://github.com/booomerang

Simple Php standalone Validator class.

  Sources   Download

MIT

The Requires

  • php >=5.3.0

 

by Alex Duplii

php validator validatr form validator