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.
You can install the package via composer:
composer require sedlatschek/laravel-conditional-equals-validation
Evaluate one other fields value.
$request->validate([
'a' => ['string', (new Equals('foo'))->if('b', 'bar')],
'b' => ['string'],
]);
Evaluate one other fields value.
$request->validate([
'a' => ['string', (new Equals('foo'))->ifNot('b', 'bar')],
'b' => ['string'],
]);
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',
];
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',
];
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',
];
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,
];
composer test
Please see CHANGELOG for more information on what has changed recently.
The MIT License (MIT). Please see License File for more information.