This package was born out of a need to ensure that for a set of inputs exactly 1 was received. Using existing validation rules could ensure that at least 1 or no more than 1 would be received, but there wasn't a succinct way of guaranteeing exactly one.
As such, two rules were implemented. The first, require_exclusive
, ensures that exactly 1 of the inputs will be present. The second, optional_exclusive
, allows for none of the inputs to be present but if any are present there must be exactly 1.
You can install the package via composer:
composer require thedavefulton/laravel-exclusive-validation-rules
The package is configured to use Laravel's automatic discovery. However, you can manually register the service provider in the app/config.php
file:
'providers' => [
// Other Service Providers
Thedavefulton\ExclusiveValidationRulesServiceProvider::class,
],
These rules may be used like any standard validation rule.
$attributes= $request->validate([
'input1' => 'required_exclusive:input2',
'input2' => 'required_exclusive:input1',
]);
$attributes= $request->validate([
'input1' => 'optional_exclusive:input2',
'input2' => 'optional_exclusive:input1',
]);
They can also be extended to n inputs
$attributes= $request->validate([
'input1' => 'required_exclusive:input2,input3,input4',
'input2' => 'required_exclusive:input1,input3,input4',
'input3' => 'required_exclusive:input1,input2,input4',
'input4' => 'required_exclusive:input1,input2,input3',
]);
composer test
Please see CHANGELOG for more information what has changed recently.
Please see CONTRIBUTING for details.
If you discover any security related issues, please email [email protected] instead of using the issue tracker.
The MIT License (MIT). Please see License File for more information.
This package was generated using the Laravel Package Boilerplate.