2017 © Pedro Peláez
 

library laravel-api-validation

Custom Validation for Laravel API requests

image

etelford/laravel-api-validation

Custom Validation for Laravel API requests

  • Monday, November 20, 2017
  • by etelford
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 2 Versions
  • 0 % Grown

The README.md

Laravel API Custom Validation

Lightweight package that allows for creating custom validation rules, mainly to be used as an alternative to Form Requests or inline Validators for API requests., (*1)

System Requirements

Laravel 5.4+ and PHP >= 7.0., (*2)

Installation

Install through Composer., (*3)

composer require etelford/laravel-api-validation

Usage

First, import the HandlesApiRequests trait (usually in your base controller):, (*4)

use Etelford\LaravelValidation\HandlesApiRequests;

For custom validation using Laravel's built-in validators, make a validation class:, (*5)

<?php

namespace App\Validation\User;

class Store extends \Etelford\LaravelValidation\BaseValidator
{
    public function rules() : array
    {
        return ['email' => 'required|email'];
    }
}

Then in your controller method, use the validation you just created:, (*6)

// UserController.php
public function store($request, $id)
{
    $this->validate($request, 'User::Store');
}

By default if validation fails, a ApiValidationException exception will be thrown., (*7)

If you wish to bypass this, you can pass a third argument to the validate() method:, (*8)

$validator = $this->validate($request, 'User::Store', $throwOnFailure = false);

From this you, you can get the Validator instance and access all of Laravel's built in Validation methods:, (*9)

if ($validator->validator()->passes()) {
    return true;
}

return false;

If you need more specialized validation that can't be handled directly by Laravel's Validator, you can create a Custom Rule and chain it to your validation call:, (*10)

class VerifyMinimum extends \Etelford\LaravelValidation\CustomRule
{
    public function passes() : bool
    {
        return $this->amount >= $this->minimum;
    }

    public function messageBag() : array
    {
        return ['amount' => 'Amount must be at least' . $this->minimum];
    }
}

$customRule = new VerifyMinimum(['amount' => 100000, 'minimum' => 50000]);
$validation = $class->validate($request, 'entity::bar')->attachRules($customRule);

The Versions

20/11 2017

dev-master

9999999-dev

Custom Validation for Laravel API requests

  Sources   Download

MIT

The Requires

 

The Development Requires

by Erik Telford

laravel api validation

20/11 2017

v0.1.0

0.1.0.0

Custom Validation for Laravel API requests

  Sources   Download

MIT

The Requires

 

The Development Requires

by Erik Telford

laravel api validation