spajz / modval by spajz

Simple model validation for Laravel 4.
11
1
1
Package Data
Maintainer Username: spajz
Maintainer Contact: rasplinjac@gmail.com (Spajz)
Package Create Date: 2014-03-25
Package Last Update: 2015-06-10
Language: PHP
License: MIT
Last Refreshed: 2024-11-19 03:12:35
Package Statistics
Total Downloads: 11
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 1
Total Watchers: 1
Total Forks: 0
Total Open Issues: 0

Simple model validation for Laravel 4


  • Auto validate model on save.
  • Use simple or separate rules for model events (create, update and save).
  • Based on Jeffrey Way project Laravel Model Validation.

Installation

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'

Usage

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

Thanks to Jeffrey Way and his project Laravel Model Validation:

https://github.com/JeffreyWay/Laravel-Model-Validation