2017 © Pedro Peláez
 

library l4-validation

Validation service class for Laravel.

image

anlutro/l4-validation

Validation service class for Laravel.

  • Friday, February 6, 2015
  • by anlutro
  • Repository
  • 2 Watchers
  • 10 Stars
  • 11,385 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 22 Versions
  • 1 % Grown

The README.md

Laravel 4 Validation Build Status Latest Version

Installation: composer require anlutro/l4-validation, (*1)

Pick the latest stable version from packagist or the GitHub tag list., (*2)

WARNING: Backwards compatibility is not guaranteed during version 0.x., (*3)

Validation service

This validation class is a layer on top of Laravel's own Validation class (the one you create by calling Validator::make), meant to be injected into a repository or controller. It allows for more advanced rulesets and more dynamic rules, and is being utilized in my repository class., (*4)

Your class must implement one abstract method: getCommonRules. This should return an array of rules that are used on every validation call and should be the bare minimum of rules. In addition you can implement as many extra rule getters as you like - getCreateRules, getUpdateRules, getUpdateAsAdminRules, getFooRules and so on., (*5)

Rules are dynamically merged depending on what action you're trying to validate. For example, $validator->validCreate($attributes) will merge getCommonRules and getCreateRules. If getCreateRules doesn't exist, it'll just use getCommonRules. $validator->validUpdateAsAdmin($attributes) will merge getCommonRules and getUpdateAsAdminRules. Rules are merged recursively., (*6)

You can tell the validator to replace variables in rules with the replace($key, $value) method. For example, if you have a unique rule and want to dynamically replace the table with a model's table, you can do the following:, (*7)

public function getCommonRules() {
    return ['email' => 'unique:<table>'];
}

$validator->replace('table', $model->getTable());

The class will also replace variables in square brackets with the matching key from input. For example, [foo] will be replaced with whatever the value of 'foo' from the provided input (attributes) are. This way, you can add the value of another input field to a rule (for example, 'end_date' => ['date', 'after:[start_date]']., (*8)

Replacing variables will not work in regex rules, as that would potentially break regex operators., (*9)

There are a couple of hooks you can use to add custom behaviour. prepareRules($rules, $attributes) will be called after rules are merged and allows you to change validation rules based on input if necessary. This method needs to return the modified array of rules!, (*10)

prepareValidator($validator) is called before checking if validation passes or not, and can be used to add sometimes() rules or any other custom behaviour onto the Illuminate\Validation\Validator object itself., (*11)

Exceptions

You can call $validatorService->toggleExceptions(); to make the validator throw exceptions instead of just returning false. The exception thrown will be of the type anlutro\LaravelValidation\ValidationException, which has some useful methods., (*12)

  • getErrors() gets a flat array of the validation errors.
  • getRules() gets the array of rules that were used when validating.
  • getAttributes() gets the array of input that was validated.

It can also be cast to a string via (string) $exception which will render each validation error on one line., (*13)

Contact

Open an issue on GitHub if you have any problems or suggestions., (*14)

License

The contents of this repository is released under the MIT license., (*15)

The Versions

06/02 2015

dev-master

9999999-dev

Validation service class for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

06/02 2015

0.5.6

0.5.6.0

Validation service class for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

27/12 2014

dev-develop

dev-develop

Validation service class for Laravel.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

19/06 2014

0.5.5

0.5.5.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

17/06 2014

0.5.4

0.5.4.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

10/06 2014

0.5.3

0.5.3.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

28/03 2014

0.5.2

0.5.2.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

26/03 2014

dev-feature/model-validation

dev-feature/model-validation

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

25/03 2014

0.5.1

0.5.1.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

21/03 2014

0.5.0

0.5.0.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

08/03 2014

0.4.4

0.4.4.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

04/03 2014

0.4.3

0.4.3.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

24/02 2014

0.4.2

0.4.2.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

24/02 2014

0.4.1

0.4.1.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

24/02 2014

0.4.0

0.4.0.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

19/02 2014

0.3.1

0.3.1.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

18/01 2014

0.3.0

0.3.0.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

16/01 2014

0.2.2

0.2.2.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

16/01 2014

0.2.1

0.2.1.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

16/01 2014

0.2.0

0.2.0.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

13/01 2014

0.1.1

0.1.1.0

Validation service class for Laravel 4.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Andreas Lutro

13/01 2014

0.1.0

0.1.0.0

Validation service class for Laravel 4.

  Sources   Download

The Requires

 

The Development Requires

by Andreas Lutro