2017 © Pedro Peláez
 

library laravel-validator-extension

An extension for Laravel4 validator.

image

kohkimakimoto/laravel-validator-extension

An extension for Laravel4 validator.

  • Sunday, August 17, 2014
  • by kohkimakimoto
  • Repository
  • 1 Watchers
  • 4 Stars
  • 206 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 4 Versions
  • 7 % Grown

The README.md

Laravel Validator Extension

Build Status Coverage Status, (*1)

An extension for Laravel4 validator., (*2)

  • Support to define validation rules in a specific class.
  • Provide another syntax to define validation rules.
  • Filter input values before and after validation.

Look at usage to get more detail., (*3)

Installation

Add dependency in composer.json, (*4)

"require": {
    "kohkimakimoto/laravel-validator-extension": "0.*"
}

Run composer upadte command., (*5)

$ composer update

Add ValidatorExtensionServiceProvider provider to providers configuration in app/config/app.php., (*6)

'providers' => array(
    ....
    'Kohkimakimoto\ValidatorExtension\ValidatorExtensionServiceProvider',
}

Add BaseValidator alias to aliases configuration in app/config/app.php., (*7)

'aliases' => array(
    ...
    'BaseValidator' => 'Kohkimakimoto\ValidatorExtension\Validator',
),

Add a path to laravel class loader in app/start/global.php., (*8)

ClassLoader::addDirectories(array(
    ...
    app_path().'/validators',
));

And add a path at autoload section in composer.json., (*9)

"autoload": {
    "classmap": [
        ...
        "app/validators"
    ]
}

Usage

Define validation rules

You can define validation rules in a validator class. If you added a path to autoload and class loader configuration at the installation steps, you can define the validator class in the app/validators directory., (*10)

// app/validators/BlogValidator.php
class BlogValidator extends BaseValidator
{
    protected function configure()
    {
        $this
            ->rule('title', 'required', 'Title is required.')
            ->rule('title', 'max:100', 'Title must not be greater than 100 characters.')
            ->rule('body', 'pass')
            ;
    }
}

You can use $this->rule method to add a validation rule. The third argument is optional to customize a error message., (*11)

The validator class is used as the below., (*12)

$validator = BlogValidator::make(Input::all());
if ($validator->fails()) {
    return Redirect::back()->withInput(Input::all())->withErrors($validator);
}

// Get only validated data.
$data = $validator->onlyValidData();

Filters

You can filter input values before and after validation., (*13)

class BlogValidator extends BaseValidator
{
    protected function configure()
    {
        $this->beforeFilter(function($validator){
            // your code
        });

        $this->afterFilter(function($validator){
            // Modify title after validation.
            $title = $validator->title;
            $title .= " created by kohki";
            $validator->title = $title;
        });
    }
}

Custom validation rules

You can define custom validation rules in the class., (*14)

class BlogValidator extends BaseValidator
{
    protected function configure()
    {
        $this
            ->rule('title', 'required', 'Title is required.')
            ->rule('title', 'max:100', 'Title must not be greater than 100 characters.')
            ->rule('body', 'foo', 'Body must be foo only!')
            ;
    }

    protected function validateFoo($attribute, $value, $parameters)
    {
        return $value == 'foo';
    }
}

Custom methods

The validator can be used as a value object. So you can append some custom methods to manipulate data stored in it., (*15)

class BlogValidator extends BaseValidator
{
    public function getTitleOrDefault() {
        if ($this->title === null) {
            return "Default title";
        } else {
            return $this->title;
        }
    }
}

LICENSE

The MIT License, (*16)

Author

Kohki Makimoto kohki.makimoto@gmail.com, (*17)

The Versions

17/08 2014