New

Just launched: Vueblocks, a brand-new search engine for Vue packages! Explore Vueblocks →

sedlatschek/laravel-conditional-equals-validation

Additional rules for conditional equals validation
3,227
Install
composer require sedlatschek/laravel-conditional-equals-validation:v1.0.0-beta.7
Latest Version:v1.0.0-beta.7
PHP:^8.1
Laravel:9 · 10
License:MIT
Last Updated:Jul 19, 2023
Links: GitHub  ·  Packagist
Maintainer: sedlatschek

Laravel Conditional Equals Validation

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides additional validation rules for Laravel projects:

Equals

$request->validate([
    'a' => ['boolean', (new Equals(true))->if('b', false)],
    'b' => ['boolean']
]);

NotEquals

$request->validate([
    'a' => ['string', (new NotEquals('foo'))->ifAnyOf(['b', 'c'], 'bar')],
    'b' => ['string'],
    'c' => ['string'],
]);

See Usage for all possibilites! Also know that the native method Rule::when may be a better alternative to this package.

Installation

You can install the package via composer:

composer require sedlatschek/laravel-conditional-equals-validation

Usage

If

Evaluate one other fields value.

$request->validate([
    'a' => ['string', (new Equals('foo'))->if('b', 'bar')],
    'b' => ['string'],
]);

IfNot

Evaluate one other fields value.

$request->validate([
    'a' => ['string', (new Equals('foo'))->ifNot('b', 'bar')],
    'b' => ['string'],
]);

IfAllOf

Evaluate if all of the given fields match the given value.

$request->validate([
    'a' => ['string', (new Equals('foo'))->ifAllOf(['b', 'c'], 'bar')],
    'b' => ['string'],
    'c' => ['string'],
]);

Example validation results:

// passes
$data = [
    'a' => 'foo',
    'b' => 'bar',
    'c' => 'bar',
];

// fails
$data = [
    'a' => 'x',
    'b' => 'bar',
    'c' => 'bar',
];

// passes
$data = [
    'a' => 'foo',
    'b' => 'bar',
    'c' => 'x',
];

IfAnyOf

Evaluate if any of the given fields match the given value.

$request->validate([
    'a' => ['string', (new Equals('foo'))->ifAnyOf(['b', 'c'], 'bar')],
    'b' => ['string'],
    'c' => ['string'],
]);

Example validation results:

// passes
$data = [
    'a' => 'foo',
    'b' => 'bar',
    'c' => 'x',
];

// fails
$data = [
    'a' => 'x',
    'b' => 'bar',
    'c' => 'x',
];

// passes
$data = [
    'a' => 'foo',
    'b' => 'x',
    'c' => 'x',
];

IfNoneOf

Evaluate if none of the given fields match the given value.

$request->validate([
    'a' => ['string', (new Equals('foo'))->ifNoneOf(['b', 'c'], 'bar')],
    'b' => ['string'],
    'c' => ['string'],
]);

Example validation results:

// passes
$data = [
    'a' => 'foo',
    'b' => 'x',
    'c' => 'x',
];

// fails
$data = [
    'a' => 'x',
    'b' => 'x',
    'c' => 'x',
];

// passes
$data = [
    'a' => 'foo',
    'b' => 'x',
    'c' => 'bar',
];

Combined

All of the above conditions can be chained. The connection between each condition is seen as an and operator.

$request->validate([
    'a' => ['string', (new Equals('foo'))->if('b', 'bar')->ifAnyOf(['c', 'd'], false)->ifAllOf(['e', 'f', 'g'], 1),
    'b' => ['string'],
    'c' => ['boolean'],
    'd' => ['boolean'],
    'e' => ['integer'],
    'f' => ['integer'],
    'g' => ['integer'],
]);

Example validation results:

// passes
$data = [
    'a' => 'foo',
    'b' => 'bar',
    'c' => true,
    'd' => false,
    'e' => 1,
    'f' => 1,
    'g' => 1,
];

// fails
$data = [
    'a' => 'x',
    'b' => 'bar',
    'c' => true,
    'd' => false,
    'e' => 1,
    'f' => 1,
    'g' => 1,
];

// passes
$data = [
    'a' => 'x',
    'b' => 'bar',
    'c' => true,
    'd' => false,
    'e' => 1,
    'f' => 1,
    'g' => 2,
];

// passes
$data = [
    'a' => 'x',
    'b' => 'x',
    'c' => true,
    'd' => false,
    'e' => 1,
    'f' => 1,
    'g' => 1,
];

// passes
$data = [
    'a' => 'x',
    'b' => 'x',
    'c' => true,
    'd' => true,
    'e' => 1,
    'f' => 1,
    'g' => 1,
];

Testing

composer test

Changelog

Please see CHANGELOG for more information on what has changed recently.

Credits

License

The MIT License (MIT). Please see License File for more information.

Related Packages

claraleigh/naughty-username

Validate that a username is not a naughty one

24,470 0
cohensive/validation

Extra functionality for Laravel 5 Validator.

13,938 14
jedrzej/validator-extended-syntax

Laravel 4/5 package that adds validator negation to Laravel's validation module

11,601 3
shin1x1/laravel-validator-builder

Laravel 4 Validator builder

7,436 2
digitonic/validation

A Laravel custom validation package with some out of the box validators.

2,526 2

Version History

Pre-releases (7)
Version Released PHP Laravel License
v1.0.0-beta.7 ^8.1 ^9.0|^10.0 MIT
v1.0.0-beta.6 ^8.1 ^9.0|^10.0 MIT
v1.0.0-beta.5 ^8.1 ^9.0|^10.0 MIT
v1.0.0-beta.4 ^8.1 ^9.0|^10.0 MIT
v1.0.0-beta.3 ^8.1 ^9.0|^10.0 MIT