All Projects → monooso → apposite

monooso / apposite

Licence: MIT license
Conditionally apply Laravel validation rules, even when you don't have access to the validator instance.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to apposite

firebase-php
Firebase Realtime Database PHP Wrapper
Stars: ✭ 41 (+13.89%)
Mutual labels:  laravel-package
laravel-snowflake
This Laravel package to generate 64 bit identifier like the snowflake within Twitter.
Stars: ✭ 94 (+161.11%)
Mutual labels:  laravel-package
bread-templates
BREAD Templates with Voyager
Stars: ✭ 19 (-47.22%)
Mutual labels:  laravel-package
simple-recaptcha-v3
🤖 This repository contains simple reCAPTCHA v3 integration for your Laravel application.
Stars: ✭ 25 (-30.56%)
Mutual labels:  laravel-package
voyager-page-blocks
A module to provide page blocks for Voyager 📝
Stars: ✭ 80 (+122.22%)
Mutual labels:  laravel-package
lastfm
🎶 Last.fm API client for PHP. Comes with a Laravel service provider.
Stars: ✭ 17 (-52.78%)
Mutual labels:  laravel-package
aliyun-oss-laravel
Laravel 的 Aliyun OSS 扩展, 支持 Laravel 9. Alibaba Cloud Object Storage Service For Laravel.
Stars: ✭ 91 (+152.78%)
Mutual labels:  laravel-package
laravel-test-watcher
Laravel Test Watcher
Stars: ✭ 20 (-44.44%)
Mutual labels:  laravel-package
laravel route summary
Create a summary of all the laravel routes
Stars: ✭ 11 (-69.44%)
Mutual labels:  laravel-package
pushbullet
Pushbullet notifications channel for Laravel
Stars: ✭ 14 (-61.11%)
Mutual labels:  laravel-package
voyager-portfolio
A Portfolio Module for Laravel Voyager 💋
Stars: ✭ 15 (-58.33%)
Mutual labels:  laravel-package
laravel-smart-facades
Strategy design pattern in laravel, the easiest way.
Stars: ✭ 84 (+133.33%)
Mutual labels:  laravel-package
laravel-charts-css
Laravel component to create gorgeous Charts.css charts.
Stars: ✭ 105 (+191.67%)
Mutual labels:  laravel-package
laracash
PHP Laravel Money Package 💰
Stars: ✭ 52 (+44.44%)
Mutual labels:  laravel-package
spid-laravel
SPID authentication package for Laravel
Stars: ✭ 41 (+13.89%)
Mutual labels:  laravel-package
apitest
this is a tool for testing Laravel REST API
Stars: ✭ 11 (-69.44%)
Mutual labels:  laravel-package
laravel-ab
Laravel A/B experiment testing tool
Stars: ✭ 108 (+200%)
Mutual labels:  laravel-package
airbrake-laravel
Laravel package for the Airbrake API, which supports Errbit
Stars: ✭ 16 (-55.56%)
Mutual labels:  laravel-package
laravel-embed
Effortless responsive embeds for videos, slideshows and more.
Stars: ✭ 106 (+194.44%)
Mutual labels:  laravel-package
laravel-dadata
PHP SDK Laravel пакет работы с сервисом DaData.ru, для исправления синтаксических ошибок в информации контактных данных клиентов сайта и вывода подсказок поля форм.
Stars: ✭ 39 (+8.33%)
Mutual labels:  laravel-package

Apposite

Lint and Test 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

Select the appropriate branch for your version of Laravel.

Branch Laravel Versions PHP Version
1.x ^6.0 ^7.2
2.x ^7.0 ^7.2.5
3.x ^8.0 ^7.3
4.x ^8.0 ^8.0

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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].