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

v5.0.0(1mo ago)341.9k3[1 PRs](https://github.com/monooso/apposite/pulls)MITPHPPHP ^8.3CI passing

Since Sep 21Pushed 3w 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 main Synced today

READMEChangelog (6)Dependencies (12)Versions (23)Used By (0)

Apposite
========

[](#apposite)

 [![Lint and Test Status](https://github.com/monooso/apposite/actions/workflows/lint-and-test.yml/badge.svg)](https://github.com/monooso/apposite/actions/workflows/lint-and-test.yml) [![Latest Stable Version](https://camo.githubusercontent.com/d64303ec47327f0b6e0053c476ca71e490dc6166a5184fbd0574a9ace8c957ed/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f6d6f6e6f6f736f2f6170706f73697465)](https://packagist.org/packages/monooso/apposite) [![License](https://camo.githubusercontent.com/bd6948af5e9510d6a25018e82b84eb49cec085d960cab5d85e2b2bce984fce98/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f6d6f6e6f6f736f2f6170706f73697465)](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)

Apposite supports only the latest major version of [Laravel](https://laravel.com/). See [`composer.json`](composer.json) for the exact version requirements.

Install it using [Composer](https://getcomposer.org/):

```
composer require monooso/apposite
```

Usage
-----

[](#usage)

Apposite provides three [custom Laravel validation rules](https://laravel.com/docs/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/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.

Local development
-----------------

[](#local-development)

Development and testing happen entirely inside [Podman](https://podman.io/) containers, driven by the `./dev` script. You do **not** need to install PHP, Composer, or any project dependencies on your machine.

The `dev` helper script builds a disposable PHP + Composer image for the PHP version you name, mounts your working tree into it, and runs your command. Any changes to `composer.json` and `composer.lock` are written back to your disk so they can be committed.

### Prerequisites

[](#prerequisites)

[Podman](https://podman.io/) 5.x or later.

### Common tasks

[](#common-tasks)

The `dev` script always accepts the target PHP version as the first argument. The first time you run a command with a given PHP version, it builds the image automatically.

CommandPurpose`./dev 8.3`Open a shell on PHP 8.3`./dev 8.3 test`Run the test suite`./dev 8.3 lint`Run Laravel Pint`./dev 8.3 composer install`Install dependencies from the lock file`./dev 8.3 composer update`Re-resolve dependencies (rewrites `composer.lock`)`./dev 8.3 composer outdated`List outdated packages`./dev 8.3 php -v`Run any command in the container`./dev build 8.3`Rebuild the image for a versionRun `./dev --help` for the full reference.

### Upgrading PHP or Laravel

[](#upgrading-php-or-laravel)

1. Create a branch.
2. Edit `composer.json` to widen the relevant constraints, e.g. `"php": "^8.3"` and `"illuminate/support": "^13.0"`.
3. Re-resolve dependencies against the target PHP version:

    ```
    ./dev 8.3 composer update
    ```
4. In the event of a conflict, investigate and iterate:

    ```
    ./dev 8.3 composer why-not laravel/framework 13.0.0
    ./dev 8.3 composer require illuminate/support:"^13.0" --no-update
    ./dev 8.3 composer update
    ```
5. Run the tests and linter:

    ```
    ./dev 8.3 test
    ./dev 8.3 lint
    ```
6. Commit `composer.json` and `composer.lock`.

Each PHP version keeps its own dependency cache, so several versions can be tested side by side (e.g. `./dev 8.2 test`, `./dev 8.3 test`) without interfering with each other.

License
-------

[](#license)

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

###  Health Score

58

—

FairBetter than 98% of packages

Maintenance94

Actively maintained with recent releases

Popularity27

Limited adoption so far

Community12

Small or concentrated contributor base

Maturity83

Battle-tested with a long release history

 Bus Factor1

Top contributor holds 81% 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 ~191 days

Recently: every ~523 days

Total

14

Last Release

32d 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

4.x-dev → v5.0.02026-07-14

PHP version history (5 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

v5.0.0PHP ^8.3

### 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 (68 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 StyleLaravel Pint

### 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

[psalm/plugin-laravel

Psalm plugin for Laravel

3345.4M354](/packages/psalm-plugin-laravel)[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.2k99.8M353](/packages/laravel-horizon)[laravel/sail

Docker files for running a basic Laravel application.

1.9k212.4M1.4k](/packages/laravel-sail)[illuminate/database

The Illuminate Database package.

2.8k55.8M13.0k](/packages/illuminate-database)[laravel/ai

The official AI SDK for Laravel.

1.1k4.6M309](/packages/laravel-ai)[moonshine/moonshine

Laravel administration panel

1.3k268.2k88](/packages/moonshine-moonshine)

PHPackages © 2026

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