bachilli/laravel-request-rules

Stop duplicating code and validations for FormRequests. This package provides reuse of FormRequest components in a generic way!

1.1.0 2020-03-03 18:30 UTC

This package is auto-updated.

Last update: 2024-04-29 04:20:18 UTC


README

Latest Version on Packagist Build Status Quality Score Total Downloads

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.