forumhouse-oss / laravel-validator by FractalizeR

A bunch of Laravel validators
772
3
5
Package Data
Maintainer Username: FractalizeR
Package Create Date: 2015-03-20
Package Last Update: 2022-09-13
Home Page: https://packagist.org/packages/fhteam/laravel-validator
Language: PHP
License: GPL-2.0-only
Last Refreshed: 2024-11-19 03:11:46
Package Statistics
Total Downloads: 772
Monthly Downloads: 1
Daily Downloads: 0
Total Stars: 3
Total Watchers: 5
Total Forks: 0
Total Open Issues: 5

Laravel validation package

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);
    }