https://github.com/monooso/apposite
Conditionally apply Laravel validation rules, even when you don't have access to the validator instance.
https://github.com/monooso/apposite
laravel laravel-package php validation
Last synced: 6 months ago
JSON representation
Conditionally apply Laravel validation rules, even when you don't have access to the validator instance.
- Host: GitHub
- URL: https://github.com/monooso/apposite
- Owner: monooso
- License: mit
- Created: 2019-09-21T20:33:23.000Z (almost 7 years ago)
- Default Branch: 4.x
- Last Pushed: 2025-11-26T21:58:13.000Z (7 months ago)
- Last Synced: 2025-11-27T15:30:27.750Z (7 months ago)
- Topics: laravel, laravel-package, php, validation
- Language: PHP
- Size: 545 KB
- Stars: 34
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# 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
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](https://getcomposer.org/):
```bash
composer require monooso/apposite
```
## 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`
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:
```php
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.
```php
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:
```php
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`
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`.
```php
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](https://github.com/monooso/apposite/blob/stable/LICENSE.txt).