PHP code example of sedlatschek / laravel-conditional-equals-validation

1. Go to this page and download the library: Download sedlatschek/laravel-conditional-equals-validation library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

sedlatschek / laravel-conditional-equals-validation example snippets


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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

$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'],
]);

// 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,
];