revati / laravel-validation by revati

Laravel validation extension for throwing ValidationException on error
5
0
2
Package Data
Maintainer Username: revati
Maintainer Contact: rgulans@gmail.com (revati)
Package Create Date: 2014-01-30
Package Last Update: 2014-02-02
Language: PHP
License: Unknown
Last Refreshed: 2024-11-23 03:18:24
Package Statistics
Total Downloads: 5
Monthly Downloads: 0
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Exceptional Validation

Validation support in two ways.

  • ValidationException throwing and configurable responses
  • BaseValidator abstract class with handy functionality

Possibilities

  • No need to worry about what happens if validation fails (ValidationException).
  • Can dynamicly assign modifiers to validation rules:
// In ResourceValidator that extends Revati\Validation\BaseValidator
$rules = [
    'title' => 'required'
    'endDate'  => 'after:[afterDate]'
];

$resourceValidator->addModifier('date', 'afterDate', Input::get('startDate'));

// So [afterDate] will be replaced with 3th parameter

Installation

Begin by installing this package through Composer. Edit your project's composer.json file to require revati/validation.

"require": {
    "laravel/framework": "4.1.*",
    "revati/validation": "dev-master"
}

Next, update Composer from the Terminal:

composer update

Once this operation completes, the final step is to add the service provider. Open app/config/app.php, and add a new item to the providers array.

// 'Illuminate\Validation\ValidationServiceProvider',
'Revati\Validation\ValidationServiceProvider'

That's it! You're all set to go.

BaseValidator

  • Create seperate Validator file for each Resource (should extend Revati\Validation\BaseValidator)
  • Set all rules that should be applied to Resource

Get specific rules

  • getRules method accepts one parameter (array) with list of all rules that should extracted from global $rules.
// Will get only title rules
$validator->getRules(array('title'));

// Will get title rules and overwrite description rules
$validator->getRules(array('title', 'description' => 'min:200'));

Dynamic rules modifiers

In url specify modifier hook like so: [modifier].

// Change hook value
// Modifier should bee passed without square brackets
$validator->addModifier('field', 'min', 20);

// As field name can be passed `*` to search in all field rules.
$validator->addModifier('*', 'max', 100);

// Modifiers can be be grouped
$validator->addModifier('field', 'min|max', 10);

ValidationException

Caution: By default exception throwing is enabled. So if you dont need it it can be disaled via packages config.

php artisan config:publish revati/validation

Whatever where you want to validate something

// ...

// Validate $input against $rules like usual.
// No need to catch response because if errors will occur, exception
// will be thrown and necessary response returned.
Validator::make($input, $rules);

// If got to this point validation passed
$user = User::create($input);

// For validation only (without throwing exception)
$validator = Validator::sotfMake($input, $rules);

// To disable or enable exception throwing at runtime use fallowing methods:
Validation::disable();
Validation::enable();

// To change response at runtime use fallowing method
Validation::setResponse($responseClosure, $ajaxResponseClosure);

// Change only one response
Validation::setResponse($responseClosure);
Validation::setResponse(null, $ajaxResponseClosure);

// ...

If error occur, fallowing responses will be returned:

// For "regular" request
return \Redirect::back()
    ->withInput()
    ->withErrors($e->getErrors());

// For ajax request
return \Response::json(array(
    'success' => false,
    'errors' => $e->getErrors()->toJson()
), 400);

// $e stands for ValidationException witch (for now) only has one method
// to return errors -> getErrors.

Customize

Those responses are defined as closures in package config file, so just by publishing package config you can modify what your app should be doing in witch case.

php artisan config:publish revati/validation

TODO

It is probably out of scope for this package, but planing to add simple js file for automating form submit + error showing on error response.