pbrus / validation-engine

A validation engine written in PHP OOP

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validation-engine

Written in PHP License

This is a simple validation engine written in PHP OOP for fun. If you need a robust PHP validator I recommend this one.

Installation

Install the package using Composer:

$ composer require pbrus/validation-engine=dev-master

Then go to the validation-engine/ directory and make dump-autoload:

$ cd vendor/pbrus/validation-engine/
$ composer dump-autoload

If don't know what Composer is, see the simplified PHP installation.

Usage

Define a validator and set its labels (here username and id):

$validator = new ValidationEngine();

$validator->setLabels(array(
    'username',
    'id'
));

In the next step set constraints for each label:

$validator->setConstraints('username', array(
    new NotEmptyValidator(array(
        'notEmptyMessage' => 'Field username must be filled out'
    )),
    new LengthValidator(array(
        'minLength' => 3,
        'maxLength' => 25,
        'lengthMessage' => "Field username must contain 3-25 letters"
    ))
));

$validator->setConstraints('id', array(
    new NotEmptyValidator(array(
        'notEmptyMessage' => "You must type your ID"
    )),
    new LengthValidator(array(
        'minLength' => 10,
        'maxLength' => 10,
        'lengthMessage' => "Field ID must consist of 10 integers"
    ))
));

Then we can validate data:

if (!$validator->setFields(array(
    'username' => 'John',   // not validated: 'Jo'
    'id' => '9876543210'    // not validated: '123'
))
) {
    echo $validator->getErrorMessage();
}

If you encounter any problems, please see the demo file index.php.

Classes

The following examples of classes are defined:

  • NotEmptyValidator
  • LengthValidator

Feel free to add own classes.

License

Validation-engine is licensed under the MIT license.

About

A validation engine written in PHP OOP

License:MIT License


Languages

Language:PHP 100.0%