browner12/validation

This package is abandoned and no longer maintained. No replacement package was suggested.

validation package for use in Laravel projects

v2.1.0 2020-03-09 02:39 UTC

This package is auto-updated.

Last update: 2024-03-14 18:28:04 UTC


README

Latest Version on Packagist Software License Build Status Coverage Status Quality Score Total Downloads

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.

Install

$ composer require browner12/validation

Setup

Add the service provider to the providers array in config/app.php.

'providers' => [
    browner12\validation\ValidationServiceProvider::class,
];

Usage

Use Artisan to generate a new validator.

php artisan make:validator UserValidator

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.

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.

namespace App\Services;

use App\Validation\ProductValidator;
use browner12\validation\ValidationException;

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

    /**
     * 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());
    }
}

Finally, your controller will call the service, and handle any errors that are thrown.

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.

Testing

$ composer test

Contributing

Please see CONTRIBUTING and CONDUCT for details.

Security

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

Credits

License

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