monooso/apposite

Conditionally apply validation rules in Laravel

v1.2.0 2019-09-28 16:47 UTC

README

Build Status Quality Score Coverage Latest Stable Version License

About Apposite

Apposite makes it easy to conditionally apply Laravel validation rules, even when you don't have access to the validator instance.

Requirements and installation

Apposite requires PHP 7.2, and has been tested against Laravel 6. It should work just fine with any recent version from the 5.x branch, but it hasn't been tested in that environment.

Install Apposite using Composer:

composer require monooso/apposite

Usage

Apposite provides three custom Laravel validation rules:

ApplyWhen

Use ApplyWhen to apply one or more validation rules when a condition is met. For example, validate the email field if the contact_method is "email".

The ApplyWhen constructor expects two arguments:

  • A conditional, which determines whether the validation rules are applied. This may be a boolean value, or a closure which returns a boolean.
  • The validation rules to apply if the conditional evaluates to true. The may be in any format recognised by the Laravel validator.

For example:

new ApplyWhen($foo === $bar, 'required|string|min:10');

new ApplyWhen(function () {
    return random_int(1, 10) % 2 === 0;
}, ['required', 'string', 'min:10']);

Add the ApplyWhen rule to your validation rules array in the normal way.

public function store(Request $request)
{
    $rules = [
        'contact_method' => ['required', 'in:email,phone'],
        'email'          => [
            new ApplyWhen($request->contact_method === 'email', ['required', 'email', 'max:255']),
        ],
    ];

    $validated = $this->validate($rules);
}

ApplyUnless

ApplyUnless is the opposite of ApplyWhen. Use it to apply one or more validation rules when a condition is not met.

For example:

public function store(Request $request)
{
    $rules = [
        'contact_method' => ['required', 'in:email,phone'],
        'email'          => [
            new ApplyUnless($request->contact_method === 'phone', ['required', 'email', 'max:255']),
        ],
    ];

    $validated = $this->validate($rules);
}

Refer to the ApplyWhen documentation for full usage instructions.

ApplyMap

Use ApplyMap when you need to choose between different sets of validation rules. For example, when validating that the chosen delivery_service is offered by the chosen delivery_provider.

public function store(Request $request)
{
    $rules = [
        'delivery_provider' => ['required', 'in:fedex,ups,usps'],
        'delivery_service'  => [
            'required',
            new ApplyMap($request->delivery_provider, [
                'fedex' => 'in:one_day,two_day',
                'ups'   => 'in:overnight,standard',
                'usps'  => 'in:two_day,someday',
            ]),
        ],
    ]; 

    $validated = $this->validate($rules);
}

The ApplyMap constructor expects two arguments:

  • The "key" value, which determines which rules to apply (if any). For example, "fedex".
  • A "map" of keys to validation rules. The validation rules may be in any format recognised by the Laravel validator.

License

Apposite is open source software, released under the MIT license.