Download the PHP package kdabrow/validation-codes without Composer
On this page you can find all versions of the php package kdabrow/validation-codes. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download kdabrow/validation-codes
More information about kdabrow/validation-codes
Files in kdabrow/validation-codes
Informations about the package validation-codes
Validation Codes for Laravel
This package enhances Laravel's validation error responses (status 422) by adding corresponding validation rule codes. After installation, the response format is as follows:
Error codes allow clients of your API to easily interpret returned validation errors in the way they want or need.
Installation
First, install the package using Composer:
PHP | Laravel | Package | Command |
---|---|---|---|
^8.3 | 12 | 3.0 | |
^8.2 | 11 | 2.0 | |
^8.1 | 10 | 1.1 |
How It Works
This package extends Laravel's default validation system by overwriting the default Handler to return the correct JSON response.
Configuration
Overwriting Validation Codes
To publish the configuration and language files containing the codes, use Laravel's command:
You can then change the validation codes corresponding to the given rules in the published file, which looks like this:
Returning Only Validation Codes (Without Messages)
To return only validation codes, set show_only_codes
to true
in the config/validation_codes.php
file. The response will be:
Caution: This might be a breaking change for your API.
Ensure your Exception\Handler
extends Kdabrow\ValidationCodes\Handler
.
Adding an Error Code to Custom Validation Rules
Add a static method getCode
to your custom validation rule. For example:
Adding an Error Code to a Validation Rule that Extends Validator
Add a fourth parameter to the extend
function:
Adding an Error Code to an Anonymous Validation Function
This is not supported.
Testing
To run tests, use the following command: