Download the PHP package indexzer0/laravel-validation-provider without Composer
On this page you can find all versions of the php package indexzer0/laravel-validation-provider. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download indexzer0/laravel-validation-provider
More information about indexzer0/laravel-validation-provider
Files in indexzer0/laravel-validation-provider
Package laravel-validation-provider
Short Description Simple reusable composable validation providers
License MIT
Homepage https://github.com/indexzer0/laravel-validation-provider
Informations about the package laravel-validation-provider
laravel-validation-provider
- Write all your validation rules in clean reusable composable providers.
- Standardise the way you define and use validation in
Form Requests
and elsewhere. - Easily compose validation rules using multiple validation providers.
- Conveniently create and validate data straight from the
ValidationProvider
.
Simple example
- Nesting domain model rules within arrays.
- Simple Example
- Requirements
- Installation
- Usage
- Defining Validation Providers
- Via Properties
- Via Methods
- Creating Validation Providers
- Facade
- Manual Creation
- Fluent API
- Service/Action Class Usage
- Form Requests Usage
- Extending Abstract
- Decorate With Trait
- Available Validation Providers
- Aggregate Validation Provider
- Nested Validation Provider
- Array Validation Provider
- Custom Validation Provider
- Exclude Attributes Validation Provider
- Map Attributes Validation Provider
- Digging Deeper
- Using Fluent API
- Using Facade
- Composing Validation Providers
- Dependent Rules
- Error Handling
- Package Offering
- Changelog
- Contributing
Requirements
laravel-validation-provider | PHP Version | Laravel Version |
---|---|---|
2.x | 8.1+ | 10.x |
3.x | 8.2+ | 11.x |
Installation
You can install the package via composer:
Usage
Defining Validation Providers
- Create granular representations of domain concepts in validation provider classes.
- Should extend
AbstractValidationProvider
.
- Should extend
Via Properties
$rules
, $messages
, $attributes
Via Methods
- You can also define methods
rules()
,messages()
,attributes()
.- Sometimes you need to dynamically define rules, messages and attributes.
- You are using a dependent rule.
- See Dependent Rules for more info.
Creating Validation Providers
There are 3 ways to create validation providers. Facade
, Manual Creation
, and Fluent API
.
In all 3 examples, were going to use the following two defined validation providers along-side this packages core validation providers to achieve validation rules of:
- Facade
- Manual Creation
- Fluent API
Facade
Manual Creation
Fluent API
- For the fluent API, you compose your validation providers from bottom up.
Service/Action Class Usage
In your services and actions ->createValidator()
and ->validate()
methods are provided for convenience.
Form Requests Usage
You can use validation providers in your form requests via two methods.
- Extending Abstract
- Decorate With Trait
Extending Abstract
ValidationProviderFormRequest
is provided to extend your form requests from.
Using prepareForValidation
hook to instantiate validation provider.
Or using dependency injection.
Decorate With Trait
HasValidationProvider
is provided to decorate your existing form requests.
Sometimes you don't have the ability to extend ValidationProviderFormRequest
. You can instead use the HasValidationProvider
trait in your existing form request.
Available Validation Providers
This package provides core classes that give you the ability to compose your validation providers.
- Aggregate Validation Provider
- Nested Validation Provider
- Array Validation Provider
- Custom Validation Provider
- Exclude Attributes Validation Provider
- Map Attributes Validation Provider
Aggregate Validation Provider
- Used when composing validation providers next to each other.
Nested Validation Provider
- Used when wanting to nest a validation provider inside an array.
Array Validation Provider
- Used when validating an array of domain models.
- https://laravel.com/docs/10.x/validation#validating-nested-array-input
Custom Validation Provider
- Used when wanting to validate data without creating a dedicated ValidationProvider class.
Exclude Attributes Validation Provider
- Sometimes you may want to remove certain attributes from a validation provider.
Map Attributes Validation Provider
- Sometimes you may want to rename an attribute.
Digging Deeper
Using Fluent API
Method | Returns |
---|---|
nested(string $nestedKey) |
NestedValidationProvider |
nestedArray(string $nestedKey) |
ArrayValidationProvider |
with(string|ValidationProvider $validationProvider) |
AggregateValidationProvider |
exclude(array $attributes) |
ExcludeAttributesValidationProvider |
map(array $attributes) |
MapAttributesValidationProvider |
Using Facade
ValidationProvider::make(ValidationProviderInterface|string|array $config): ValidationProviderInterface
-
Can use fully qualified class name strings.
-
Invalid class string throws exception.
-
Can use validation provider objects. Essentially does nothing.
-
Can use arrays (fully qualified class name strings and objects).
-
Array string keys create
NestedValidationProvider
. - Empty array is invalid.
Composing Validation Providers
Use case:
- You may have parts of your application that need to validate data for multiple domain concepts.
- You may want to validate data in nested arrays without introducing duplication in your rule definitions.
Example:
Let's look at the example of 3 routes and how you could reuse your Validation Providers.
-
Route: address
- Stores address information
- Uses
AddressValidationProvider
-
Route: contact-details
- Stores contact information
- Uses
ContactValidationProvider
- Route: profile
- Stores address and contact information.
- Uses
AddressValidationProvider
ContactValidationProvider
NestedValidationProvider
(under the hood from Facade)AggregateValidationProvider
(under the hood from Facade)
Dependent Rules
- When using any of the dependent rules, you should use the
$this->dependentField()
helper.- This ensures that when using the
NestedValidationProvider
andArrayValidationProvider
, the dependent field will have the correct nesting.
- This ensures that when using the
Error Handling
All exceptions thrown by the package implement \IndexZer0\LaravelValidationProvider\Contracts\ValidationProviderExceptionInterface
.
How-ever it doesn't harm to also catch \Throwable
.
Package Offering
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Credits
- IndexZer0
- All Contributors
License
The MIT License (MIT). Please see License File for more information.