Code Monkey home page Code Monkey logo

vlad's Introduction

Vlad

Build Status Coverage Status

Input validation library, that has inbuilt error messages that are translatable, validators that are easy to extend, and that has easy to understand test declaration syntax.

Succint Test Declaration

Test is composed of assertions about the input.

$test = new \Gajus\Vlad\Test();

$test
    // string $selector_name[, boolean $condition = false]
    ->assert('user[first_name]')
    // string $validator_name[, array $validator_options = null[, array $condition_options = null]]
    ->is('NotEmpty')
    ->is('String')
    ->is('LengthMin', ['length' => 5])
    ->is('LengthMax', ['length' => 20]);

$test
    ->assert('user[last_name]')
    ->is('NotEmpty')
    ->is('String')
    ->is('LengthMin', ['length' => 5])
    ->is('LengthMax', ['length' => 20]);

if ($assessment = $test->assess($_POST)) {
    // Iterate through error messages.
    foreach ($assessment as $error) {
        // [..]
    }
}

Extendable Validation Rules

Vlad has inbuilt validators. It is easy to write custom validators. You can request new validators to be added to the core package. Validators benefit from the translator interface.

Vlad does not encourage inline boolean validation expressions.

Inbuilt Validation Rules

Validator Description
String Validate that input is a string.
Regex Validate that input is matched using a regular expression.
RangeMinInclusive Validate that a numeric input is at least of the given size (inclusive).
RangeMinExclusive Validate that a numeric input is at least of the given size (exclusive).
RangeMaxInclusive Validate that a numeric input is at most of the given size (inclusive).
RangeMaxExclusive Validate that a numeric input is at most of the given size (exclusive).
NotEmpty Validate that input value is not empty.
Length Validate that input string representation is of a specific length.
Integer Validate that input is an integer.
LengthMin Validate that input string representation is not shorter than the specified length.
LengthMax Validate that input string representation is not longer than the specified length.
In Validate that input value is in the haystack.
Email Validate that input value is syntactically valid email address.

Writing a Custom Validator

Each validator is a class that extends Gajus\Vlad\Validator. Validators that are not part of the Vlad package must be under a namespace.

<?php
namespace Foo\Bar;

class HexColor extends \Gajus\Vlad\Validator {
    static protected
        // Each option must be predefined with default value.
        $default_options = [
            'trim' => false
        ],
        $message = '{input.name} is not a hexadecimal number.';
    
    public function assess ($value) {
        $options = $this->getOptions();

        if ($options['trim']) {
            $value = ltrim($value, '#');
        }

        return ctype_xdigit($value) && (strlen($value) == 6 || strlen($value) == 3);
    }
}

In the test declaration, custom validator is referred to using the full (namespaced) class name.

$test = new \Gajus\Vlad\Test();
$test
    ->assert('foo_bar')
    ->is('Foo\Bar\HexColor');

$assessment = $test->assess(['foo_bar' => 'fff']);

Multilingual

Translator allows to overwrite default error messages and give input names.

In addition to the provided (below) use cases of the translator, you can extend Gajus\Vlad\Translator with your own functionality (e.g. importing translations from a file or database).

Input name

In most cases, you do not need to provide input name at all. Vlad will derive English name from the selector, e.g. foo[bar_tar_id] will come out as "Foo Bar Tar".

You can translate input names.

$translator = new \Gajus\Vlad\Translator();
$translator->setInputName('foo[bar_tar_id]', 'Bar Tar');

$test = new \Gajus\Vlad\Test();
$test
    ->assert('foo_bar')
    ->is('NotEmpty');

$assessment = $test->assess([]);

The above will produce the following error message:

Bar Tar is empty.

Validator Message

Validators have inbuilt English error messages. You can overwrite them like this:

$translator = new \Gajus\Vlad\Translator();
$translator->setValidatorMessage('NotEmpty', '{input.name} cannot be left empty.');

$test = new \Gajus\Vlad\Test($translator);
$test
    ->assert('foo_bar')
    ->is('NotEmpty');

$assessment = $test->assess([]);

Foo Bar cannot be left empty.

Assertion Error Message

Individual assertions can overwrite the error messages.

$test = new \Gajus\Vlad\Test();
$test
    ->assert('foo_bar')
    ->is('NotEmpty', null, ['message' => 'You must provide Foo Bar value.']);

Installation

Vlad uses Composer to install and update:

curl -s http://getcomposer.org/installer | php
php composer.phar require gajus/vlad

Todo

Alternatives

vlad's People

Contributors

gajus avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.