fhteam/laravel-validator

A bunch of Laravel validators

v2.2.0 2019-07-16 10:59 UTC

This package is not auto-updated.

Last update: 2020-06-26 18:21:40 UTC


README

Metrics _
Version PHP version
Compatibility Laravel compatibility
Quality Code Climate Build Status Coverage Status

Features:

  • Validation logic is completely separate from objects being validated
  • Validation rules and validation failure behaviour can be written in a declarative way in 90% of cases
  • Uses Laravel validator core and rules
  • Stateful model validation
  • Provides OOP way to write and register new validation rules
  • Serves as a replacer to a Input::get() to prevent unvalidated data from sneaking into your application
  • Rule-based data transformations of validated data before it reaches your application logic (in progress)
  • Provides unified way to write rules for controllers and models
  • Can validate and aggregate many types of input: $_GET/$_POST, $_FILES, HTTP headers, Session data, Cookies (both PHP native and Laravel's encrypted ones)
  • Two types of input validators: middleware (to be manually registered) and validate-when-resolved (just inject and you are done)

Documentation

  • Documentation is available here

Quick example:

class OrderControllerValidator extends FrontendControllerValidatorWhenResolved
{
    protected $rules = [
        '*' => [
            'user_id' => 'required|numeric',
        ],
        'getCreate, getShow, getEdit' => [],
        'postOpen, postDelete, postAssignContractor, postCancelContractorAssignment' => [],
        'postCreate' => [
            'title' => 'required|max:100',
            'categoryId' => 'required|numeric',
            'description' => 'required|max:4096',
        ],
        'postEdit, postFileDelete, postFileUpload' => [
            'title' => 'required|max:100',
            'categoryId' => 'required|numeric',
            'description' => 'required|max:4096',
        ],
    ];

    protected $errorRedirects = [
        'getShow' => ['route' => 'home'],
        'postCreate' => ['route' => 'orders_create'],
        'postEdit, postFileUpload, postFileDelete' => ['route' => ['orders_edit', ['orderId' => '#orderId']]],
    ];
}

class OrderController extends Controller
{
    /**
     * @var OrderControllerValidator
     */
    protected $validator;
    
    /**
     * IoC invoked constructor
     */
    public function __construct(OrderControllerValidator $validator) {
        $this->validator = $validator;
    }
    
    public function getShowValidatedData() {
        return Response::make($this->validator->description);
    }