spajz/modval is a Laravel package for simple model validation for laravel 4..
It currently has 1 GitHub stars and 12 downloads on Packagist (latest version 1).
Install it with composer require spajz/modval.
Discover more Laravel packages by spajz
or browse all Laravel packages to compare alternatives.
Last updated
Just place require new package for your Laravel installation via composer.json
"spajz/modval": "dev-master"
Then composer update.
You can add following lines to app/config/app.php if you want to use alias:
'Modval' => 'Spajz\Modval\Modval'
Your models need to extend Modval or Spajz\Modval\Modval.
class Foo extends Modval {
}
How to add rules in the model (same for all events).
class Foo extends Modval {
protected static $rules = array(
'title' => 'required',
'slug' => 'required|min:5',
);
}
Add rules separately for each event (create, update or save). Save event is executed in both cases (create and update).
class Foo extends Modval {
protected static $rules = array(
'create' => array(
'slug' => 'required|min:5',
),
'update' => array(
'url' => 'required',
),
'save' => array(
'title' => 'required',
),
);
}
And here's an example from controller.
public function store()
{
$foo = new Foo(Input::all());
if ($foo->save()) return Redirect::route('foo.index');
return Redirect::back()->withInput()->withErrors($foo->getErrors());
}
Thanks to Jeffrey Way and his project Laravel Model Validation: