Git Product home page Git Product logo

validation's Introduction

Validation

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 [email protected] instead of using the issue tracker.

Credits

License

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

validation's People

Contributors

browner12 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

validation's Issues

composer require browner12/validation

Could not find package browner12/validation at any version for your minimum-stab
ility (stable). Check the package spelling or your minimum-stability

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.