Download the PHP package tzurbaev/laravel-extra-fields-validator without Composer
On this page you can find all versions of the php package tzurbaev/laravel-extra-fields-validator. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download tzurbaev/laravel-extra-fields-validator
More information about tzurbaev/laravel-extra-fields-validator
Files in tzurbaev/laravel-extra-fields-validator
Package laravel-extra-fields-validator
Short Description Extra fields validator for Laravel.
License MIT
Informations about the package laravel-extra-fields-validator
Laravel Extra Fields Validator
Description
This package allows to perform validation of exta (redundant) fields that were sent with HTTP request.
All you need to do is create new Form Request
by extending Laravel\ExtraFieldsValidator\ExtraFormRequest
class (instead of Illuminate\Foundation\Http\FormRequest
).
Since the ExtraFormRequest
extends the original FormRequest
class, you can work with this class just as with regular
Form Request.
Behind the scenes ExtraFormRequest
creates custom instance of Validator class (Laravel\ExtraFieldsValidator\Validator
)
that allows to register & run extra callbacks (after successful validation & after failed validation).
After successful validation ExtraFormRequest
will try to find fields that are not described in rules
list
but present in actual HTTP request payload.
If there's any, response will finish with HTTP 422 Unprocessable Entity
response and custom error message
will be added to errors bag (ExtraFormRequest::getExtraFieldErrorMessage(string $field): string
).
Requirements
This package requires PHP 7.4 or higher.
Installation
You can install the package via composer:
Also you can publish package configuration:
Usage
Let's say you have the following request:
If actual HTTP request payload contains any other field, validation will fail.
Using custom validator
If you're creating custom validator via FormRequest
's validator
method, you need to use result of
parent::validator()
method as your base validator.
Using custom form request
If you're using custom FormRequest
class and can't extend from ExtraFormRequest
, you can include
Laravel\ExtraFieldsValidator\ProvidesExtraFieldsValidator
trait into your base/child class.
Error message
You need to add message to your validation.php
language file (under the custom.extra_field
path). The extra field
name will be passed as :attribute
replacement.
You can also override ExtraFormRequest::getExtraFieldErrorMessage
method and return any custom message.
Data source
By default validator uses Laravel's FormRequest::validationData()
method to retrieve data that should
be validated. If you have request with no validation rules or your route can accept optional query
params, this might lead to unexpected validation errors, since optional query params might be missing
from your rules
array.
You have 2 options to deal with this:
- Describe optional params in your
rules
method. This will not break your requests with optional query params but if there's any other non-described param, you will face validation exception; - Change the
extra-validator.data_source
(or ENV variableEXTRA_VALIDATOR_DATA_SOURCE
) value fromdefault
toinput_source
. This will instruct validator only to use JSON/Request data without query params.
GET requests
In most cases you should not use ExtraFormRequest
for your GET routes. If you really need it,
describe all available params in your rules
method.
Changelog
Please see CHANGELOG for more information on what has changed recently.
Testing
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
License
The MIT License (MIT). Please see License File for more information.