2017 © Pedro Peláez
 

library validation

validation package for use in Laravel projects

image

browner12/validation

validation package for use in Laravel projects

  • Tuesday, February 23, 2016
  • by browner12
  • Repository
  • 1 Watchers
  • 1 Stars
  • 1,038 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Validation

Latest Version on Packagist ![Software License][ico-license] Build Status ![Coverage Status][ico-scrutinizer] Quality Score ![Total Downloads][ico-downloads], (*1)

This is a validation package built to complement Laravel's included validation. One of the main benefits of this package is a separate file houses the reusable rules for validation., (*2)

Install

``` bash $ composer require browner12/validation, (*3)


## Setup Add the service provider to the providers array in `config/app.php`. ``` php 'providers' => [ browner12\validation\ValidationServiceProvider::class, ];

Usage

Use Artisan to generate a new validator., (*4)

``` sh php artisan make:validator UserValidator, (*5)


Validators extend the abstract `browner12\validation\Validator`, which contains all of the methods necessary to perform validation. The only thing you need to define are your rules. For example, if you have a 'Product' model, you could create a `ProductValidator`. While they *can* be placed anywhere that can be autoloaded, a good suggestion is `app/Validation`. ``` php namespace App\Validation; class ProductValidator extends Validator { protected static $store = [ 'name' => 'required', 'price' => 'required|int', ]; protected static $update = [ 'name' => 'required', 'price' => 'required|int', ]; }

As you can see, validators can contain multiple rule sets. To use the validator, create a new Validator object, or use dependency injection (DI is used in the example). Pass in the data to be validated and the name of the rule set to use. A good place to handle the validation is in a dedicated class (sometimes referred to as a Service) so it can be reused throughout the site., (*6)

``` php namespace App\Services;, (*7)

use App\Validation\ProductValidator; use browner12\validation\ValidationException;, (*8)

class ProductService { /** * constructor * * @param \App\Validation\ProductValidator $validator */ public function __construct(ProductValidator $validator) { $this->validator = $validator; }, (*9)

/**
 * store product
 *
 * @param array $input
 * @throws \browner12\validation\ValidationException
 */
public function store(array $input)
{
    if ($this->validator->isValid($input, 'store')) {

        //data is good, save to storage
        return true;
    }

    throw new ValidationException('Storing a product failed.', $this->validator->getErrors());
}

}, (*10)


Finally, your controller will call the service, and handle any errors that are thrown. ``` php use App\Services\ProductService; use browner12\validation\ValidationException; class ProductController { /** * constructor */ public function __construct(ProductService $service) { $this->service = $service; } /** * store */ public function store() { try { $data = [ 'name' => $_POST['name'], 'price' => $_POST['price'], ]; $this->service->store($data); } catch (ValidationException $e){ //handle the exception } } }

Changelog

Please see CHANGELOG for more information what has changed recently., (*11)

Testing

bash $ composer test, (*12)

Contributing

Please see CONTRIBUTING and CONDUCT for details., (*13)

Security

If you discover any security related issues, please email browner12@gmail.com instead of using the issue tracker., (*14)

Credits

License

The MIT License (MIT). Please see License File for more information., (*15)

The Versions

23/02 2016

dev-master

9999999-dev https://github.com/browner12/validation

validation package for use in Laravel projects

  Sources   Download

MIT

The Requires

 

The Development Requires

validation browner12

20/02 2016

v1.0.0

1.0.0.0 https://github.com/browner12/validation

validation package for use in Laravel projects

  Sources   Download

MIT

The Requires

 

The Development Requires

validation browner12