Skip to content
This repository has been archived by the owner on Dec 14, 2018. It is now read-only.

Load Laravel validator definitions from files or directories

Notifications You must be signed in to change notification settings

fortrabbit/validator-loader

Repository files navigation

Build Status

Laravel Validation Loader

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

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

For the service provider, integrating the loader neatly into Laravel, look here.

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

$ php composer.phar require "frbit/validator-loader:*"

Example

That's how you use it:

<?php

use \Frbit\ValidatorLoader\Factory;

$loader = Factory::fromFile("my-rules.yml"); # or "my-rules.json" or "my-rules.php"

$inputData = array('email' => '[email protected]');
$validator = $loader->get('my-form', $inputData);

if ($validator->fails()) {
    # ..
}

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

---

variables:
    FOO: /foo/
    bar: 5

validators:

    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

use \Frbit\ValidatorLoader\Factory;

$loader = Factory::fromDirectory("my-directory");

Validator definition from array

If you need the flexibility.

<?php

use \Frbit\ValidatorLoader\Factory;

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

#Using custom validation methods

Custom validation rules, with the same signature as the Validator::extend method, can be added either in the definition (file) or programmatically. Once added, they are automatically available in all named validators.

Definition file

---

methods:
    foo: FooValidator@validate
variables:
    FOO: /foo/
    bar: 5
validators:
    my-form:
        rules:
            email:
                - foo
        messages:
            email.foo: EMail is not foo

Programmatically

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

About

Load Laravel validator definitions from files or directories

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages