Package Data | |
---|---|
Maintainer Username: | kohkimakimoto |
Maintainer Contact: | kohki.makimoto@gmail.com (Kohki Makimoto) |
Package Create Date: | 2014-08-15 |
Package Last Update: | 2014-08-17 |
Language: | PHP |
License: | MIT |
Last Refreshed: | 2024-11-22 03:15:07 |
Package Statistics | |
---|---|
Total Downloads: | 235 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 4 |
Total Watchers: | 2 |
Total Forks: | 0 |
Total Open Issues: | 0 |
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 kohki.makimoto@gmail.com