2017 © Pedro Peláez
 

library validator-loader

Load Laravel validator definitions from files or directories

image

frbit/validator-loader

Load Laravel validator definitions from files or directories

  • Tuesday, September 2, 2014
  • by ukautz
  • Repository
  • 3 Watchers
  • 10 Stars
  • 83 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 2 Forks
  • 0 Open issues
  • 5 Versions
  • 0 % Grown

The README.md

Build Status, (*1)

Laravel Validation Loader

Load Laravel validator definitions from files or directories (or wherever you want to store them)., (*2)

The general idea is to centralize validation (rules) to simplify maintenance and maximize re-usability., (*3)

For the service provider, integrating the loader neatly into Laravel, look here., (*4)

Features

  • Provides simple inheritance, for re-usable validation rules
  • Allows variable usage, to keep the validations clean and readable
  • Supports out of the box: JSON, YAML or native PHP files. Or directories containing such files.
  • Flexible, extendable interface
  • Can be used outside of Laravel

Installation

``` bash $ php composer.phar require "frbit/validator-loader:*", (*5)


## Example That's how you use it: ``` php <?php use \Frbit\ValidatorLoader\Factory; $loader = Factory::fromFile("my-rules.yml"); # or "my-rules.json" or "my-rules.php" $inputData = array('email' => 'foo@bar.tld'); $validator = $loader->get('my-form', $inputData); if ($validator->fails()) { # .. }

And this is how the file (structure) looks like:, (*6)

``` yaml

variables: FOO: /foo/ bar: 5, (*7)

validators:, (*8)

my-form:
    rules:
        email:
            - min:10

            # use the bar variable
            - max:<<bar>>

            # use the FOO variable
            - regex:<<FOO>>

    messages:
        email.min: EMail too short
        email.max: EMail too long
        email.regex: EMail not foo enough

other-form:

    # extend from above
    extends: my-form

    rules:
        email:
            # just differ in the min rule
            - min:15

    messages:
        # just differ in the regex error message
        email.regex: You are not foo

### Validator files in directories The more complex your application becomes and the more validation rules you need, the more it makes sense split the rules into multiple files. Each named validator (and variable) name must be unique across all files. ``` php <?php use \Frbit\ValidatorLoader\Factory; $loader = Factory::fromDirectory("my-directory");

Validator definition from array

If you need the flexibility., (*9)

``` php <?php, (*10)

use \Frbit\ValidatorLoader\Factory;, (*11)

$loader = $loader = Factory::fromArray(array( 'variables' => array( 'FOO' => '/foo/', 'bar' => 5 ), 'validators' => array( 'my-form' => array( 'rules' => array( 'email' => array( 'min:10', 'max:' ) ), 'messages' => array( 'email.min' => 'Too short', 'email.max' => 'Too long' ) ) ) ));, (*12)


# #Using custom validation methods [Custom validation rules](http://laravel.com/docs/validation#custom-validation-rules), with the same signature as the [Validator::extend](https://github.com/illuminate/validation/blob/master/Validator.php) method, can be added either in the definition (file) or programmatically. Once added, they are automatically available in all named validators. ### Definition file ``` yaml --- methods: foo: FooValidator@validate variables: FOO: /foo/ bar: 5 validators: my-form: rules: email: - foo messages: email.foo: EMail is not foo

Programmatically

php <?php // .. $loader->setMethod('foo', 'FooValidator@validate');, (*13)

The Versions

02/09 2014

dev-master

9999999-dev

Load Laravel validator definitions from files or directories

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel validator validation

02/09 2014

0.1.4

0.1.4.0

Load Laravel validator definitions from files or directories

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel validator validation

18/08 2014

0.1.3

0.1.3.0

Load Laravel validator definitions from files or directories

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel validator validation

14/03 2014

0.1.1

0.1.1.0

Load Laravel validator definitions from files or directories

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel validator validation

11/03 2014

0.1.0

0.1.0.0

Load Laravel validator definitions from files or directories

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel validator validation