kohkimakimoto/laravel-validator-extension

An extension for Laravel4 validator.

v0.3.0 2014-08-17 01:42 UTC

README

Build Status Coverage Status

An extension for Laravel4 validator.

  • 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.

Installation

Add dependency in composer.json

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

Run composer upadte command.

$ composer update

Add ValidatorExtensionServiceProvider provider to providers configuration in app/config/app.php.

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

Add BaseValidator alias to aliases configuration in app/config/app.php.

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

Add a path to laravel class loader in app/start/global.php.

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

And add a path at autoload section in composer.json.

"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.

// 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.

The validator class is used as the below.

$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.

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.

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.

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

LICENSE

The MIT License

Author

Kohki Makimoto kohki.makimoto@gmail.com