bachilli / laravel-request-rules
Stop duplicating code and validations for FormRequests. This package provides reuse of FormRequest components in a generic way!
Requires
- php: ^7.1
- illuminate/support: ^6.0
Requires (Dev)
- orchestra/testbench: ^4.0
- phpunit/phpunit: ^8.0
This package is auto-updated.
Last update: 2024-10-29 05:47:41 UTC
README
Stop duplicating code and validations for FormRequests. This package provides reuse of FormRequest rules in a generic way!
Installation
You can install the package via composer:
composer require bachilli/laravel-request-rules
Usage
Create your main FormRequests with all your validation rules. After that, create unique FormRequests for your controller methods. There is an example of a StoreProductRequest:
public function rules() { return RequestRule::resolveRules( [ 'name' => 'required', ], [ RequestRule::merge(VolumeEntityFormRequest::class, 'volumes.*', 'required|array')->only(['dimensions'])->get(), RequestRule::merge(ProductEntityFormRequest::class, 'products.*')->except(['price'])->get(), ]); }
The first array on resolveRules method is exclusive for your FormRequest, the second array is the composed rules, imported from other FormRequests.
Methods
There some helper methods available to use when you're importing rules. Below the description of all methods.
merge(SomeFormRequest::class, 'desired_field_name', 'optional_validations")
In the desired_field_name
you can pass with the suffix .*
to specify an array field.
The optional_validations
are useful when you need an array field and want some validation, like required
.
only(['field_name', SomeFormRequest::class])
You can import only rules if you want.
except(['field_name', SomeFormRequest::class])
You can do the opposite, importing all rules, except the specified ones.
Testing
Not implemented yet.
Changelog
Please see CHANGELOG for more information what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security
If you discover any security related issues, please email bachilli@gmail.com instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.