An extension for Laravel4 validator.
kohkimakimoto/laravel-validator-extension is a Laravel package for an extension for laravel4 validator..
It currently has 4 GitHub stars and 237 downloads on Packagist (latest version v0.3.0).
Install it with composer require kohkimakimoto/laravel-validator-extension.
Discover more Laravel packages by kohkimakimoto
or browse all Laravel packages to compare alternatives.
Last updated
An extension for Laravel4 validator.
Look at usage to get more detail.
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"
]
}
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();
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;
});
}
}
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';
}
}
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;
}
}
}
The MIT License
Kohki Makimoto [email protected]