PHPackages                             monooso/apposite - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Validation &amp; Sanitization](/categories/validation)
4. /
5. monooso/apposite

ActiveLibrary[Validation &amp; Sanitization](/categories/validation)

monooso/apposite
================

Conditionally apply validation rules in Laravel

v4.0.0(4y ago)341.9k3[3 PRs](https://github.com/monooso/apposite/pulls)MITPHPPHP ^8.0CI failing

Since Sep 21Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/monooso/apposite)[ Packagist](https://packagist.org/packages/monooso/apposite)[ Docs](https://github.com/monooso/apposite)[ RSS](/packages/monooso-apposite/feed)WikiDiscussions 4.x Synced 3d ago

READMEChangelog (5)Dependencies (6)Versions (19)Used By (0)

Apposite
========

[](#apposite)

 [![Lint and Test Status](https://github.com/monooso/unobserve/actions/workflows/lint-and-test.yml/badge.svg)](https://github.com/monooso/unobserve/actions/workflows/lint-and-test.yml/badge.svg) [![Quality Score](https://camo.githubusercontent.com/775df878e5d59012e4ca7523077d77c358af780a6370453d3b3a997414582edb/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f672f6d6f6e6f6f736f2f6170706f736974652e737667)](https://scrutinizer-ci.com/g/monooso/apposite) [![Coverage](https://camo.githubusercontent.com/80fbf5eb2c578ca8ad056201a38ef3a441afdca5307e3205781e58b3a20d4299/68747470733a2f2f696d672e736869656c64732e696f2f7363727574696e697a65722f636f7665726167652f672f6d6f6e6f6f736f2f6170706f736974652e737667)](https://scrutinizer-ci.com/g/monooso/apposite) [![Latest Stable Version](https://camo.githubusercontent.com/739c1bd38e08fd1d304af9516e206894df6e0391e2c7962d77269b9600a2687f/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6e6f6f736f2f6170706f736974652f762f737461626c652e737667)](https://packagist.org/packages/monooso/apposite) [![License](https://camo.githubusercontent.com/4a994314d82070f93249224020ed73d6c3287da87e29ab389b198218cd575e78/68747470733a2f2f706f7365722e707567782e6f72672f6d6f6e6f6f736f2f6170706f736974652f6c6963656e73652e737667)](https://packagist.org/packages/monooso/apposite)

About Apposite
--------------

[](#about-apposite)

Apposite makes it easy to conditionally apply Laravel validation rules, even when you don't have access to [the validator instance](https://laravel.com/docs/validation#conditionally-adding-rules).

Requirements and installation
-----------------------------

[](#requirements-and-installation)

Select the appropriate branch for your version of Laravel.

BranchLaravel VersionsPHP Version1.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](https://getcomposer.org/):

```
composer require monooso/apposite
```

Usage
-----

[](#usage)

Apposite provides three [custom Laravel validation rules](https://laravel.com/docs/8.x/validation#using-rule-objects):

- [`ApplyWhen`](#apply-when)
- [`ApplyUnless`](#apply-unless)
- [`ApplyMap`](#apply-map)

### `ApplyWhen`

[](#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](https://laravel.com/docs/8.x/validation#quick-writing-the-validation-logic) 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-)

`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`](#apply-when) documentation for full usage instructions.

### `ApplyMap`

[](#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
-------

[](#license)

Apposite is open source software, released under [the MIT license](https://github.com/monooso/apposite/blob/stable/LICENSE.txt).

###  Health Score

45

—

FairBetter than 92% of packages

Maintenance56

Moderate activity, may be stable

Popularity27

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity71

Established project with proven stability

 Bus Factor1

Top contributor holds 76.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~188 days

Recently: every ~477 days

Total

13

Last Release

169d ago

Major Versions

v1.2.1 → 2.x-dev2020-03-13

v2.0.2 → 3.x-dev2020-10-19

v3.0.0 → v4.0.02021-05-25

PHP version history (4 changes)v1.0.0PHP &gt;=7.2.0

2.x-devPHP &gt;=7.2.5

3.x-devPHP ^7.3

v4.0.0PHP ^8.0

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/4369838?v=4)[Stephen Lewis](/maintainers/monooso)[@monooso](https://github.com/monooso)

---

Top Contributors

[![monooso](https://avatars.githubusercontent.com/u/4369838?v=4)](https://github.com/monooso "monooso (51 commits)")[![dependabot[bot]](https://avatars.githubusercontent.com/in/29110?v=4)](https://github.com/dependabot[bot] "dependabot[bot] (16 commits)")

---

Tags

laravellaravel-packagephpvalidationlaravelvalidation

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/monooso-apposite/health.svg)

```
[![Health](https://phpackages.com/badges/monooso-apposite/health.svg)](https://phpackages.com/packages/monooso-apposite)
```

###  Alternatives

[propaganistas/laravel-phone

Adds phone number functionality to Laravel based on Google's libphonenumber API.

3.0k35.7M107](/packages/propaganistas-laravel-phone)[proengsoft/laravel-jsvalidation

Validate forms transparently with Javascript reusing your Laravel Validation Rules, Messages, and FormRequest

1.1k2.3M49](/packages/proengsoft-laravel-jsvalidation)[axlon/laravel-postal-code-validation

Worldwide postal code validation for Laravel and Lumen

3853.3M1](/packages/axlon-laravel-postal-code-validation)[stevebauman/purify

An HTML Purifier / Sanitizer for Laravel

5325.6M19](/packages/stevebauman-purify)[laravel-validation-rules/credit-card

Validate credit card number, expiration date, cvc

2412.2M5](/packages/laravel-validation-rules-credit-card)[galahad/laravel-addressing

Laravel package providing addressing functionality

70316.6k](/packages/galahad-laravel-addressing)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
