Skip to content

Commit d89400b

Browse files
Add ExcludeIf and ProhibitedIf Validation Methods (#41617)
* Add ExcludeIf and ProhibitedIf Validation Methods * formatting Co-authored-by: Taylor Otwell <[email protected]>
1 parent 45754fd commit d89400b

File tree

5 files changed

+220
-0
lines changed

5 files changed

+220
-0
lines changed

src/Illuminate/Validation/Rule.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
use Illuminate\Contracts\Support\Arrayable;
66
use Illuminate\Support\Traits\Macroable;
77
use Illuminate\Validation\Rules\Dimensions;
8+
use Illuminate\Validation\Rules\ExcludeIf;
89
use Illuminate\Validation\Rules\Exists;
910
use Illuminate\Validation\Rules\In;
1011
use Illuminate\Validation\Rules\NotIn;
12+
use Illuminate\Validation\Rules\ProhibitedIf;
1113
use Illuminate\Validation\Rules\RequiredIf;
1214
use Illuminate\Validation\Rules\Unique;
1315

@@ -103,6 +105,28 @@ public static function requiredIf($callback)
103105
return new RequiredIf($callback);
104106
}
105107

108+
/**
109+
* Get a exclude_if constraint builder instance.
110+
*
111+
* @param callable|bool $callback
112+
* @return \Illuminate\Validation\Rules\ProhibitedIf
113+
*/
114+
public static function excludeIf($callback)
115+
{
116+
return new ExcludeIf($callback);
117+
}
118+
119+
/**
120+
* Get a prohibited_if constraint builder instance.
121+
*
122+
* @param callable|bool $callback
123+
* @return \Illuminate\Validation\Rules\ProhibitedIf
124+
*/
125+
public static function prohibitedIf($callback)
126+
{
127+
return new ProhibitedIf($callback);
128+
}
129+
106130
/**
107131
* Get a unique constraint builder instance.
108132
*
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Illuminate\Validation\Rules;
4+
5+
use InvalidArgumentException;
6+
7+
class ExcludeIf
8+
{
9+
/**
10+
* The condition that validates the attribute.
11+
*
12+
* @var callable|bool
13+
*/
14+
public $condition;
15+
16+
/**
17+
* Create a new exclude validation rule based on a condition.
18+
*
19+
* @param callable|bool $condition
20+
* @return void
21+
*/
22+
public function __construct($condition)
23+
{
24+
if (! is_string($condition)) {
25+
$this->condition = $condition;
26+
} else {
27+
throw new InvalidArgumentException('The provided condition must be a callable or boolean.');
28+
}
29+
}
30+
31+
/**
32+
* Convert the rule to a validation string.
33+
*
34+
* @return string
35+
*/
36+
public function __toString()
37+
{
38+
if (is_callable($this->condition)) {
39+
return call_user_func($this->condition) ? 'exclude' : '';
40+
}
41+
42+
return $this->condition ? 'exclude' : '';
43+
}
44+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
namespace Illuminate\Validation\Rules;
4+
5+
use InvalidArgumentException;
6+
7+
class ProhibitedIf
8+
{
9+
/**
10+
* The condition that validates the attribute.
11+
*
12+
* @var callable|bool
13+
*/
14+
public $condition;
15+
16+
/**
17+
* Create a new prohibited validation rule based on a condition.
18+
*
19+
* @param callable|bool $condition
20+
* @return void
21+
*/
22+
public function __construct($condition)
23+
{
24+
if (! is_string($condition)) {
25+
$this->condition = $condition;
26+
} else {
27+
throw new InvalidArgumentException('The provided condition must be a callable or boolean.');
28+
}
29+
}
30+
31+
/**
32+
* Convert the rule to a validation string.
33+
*
34+
* @return string
35+
*/
36+
public function __toString()
37+
{
38+
if (is_callable($this->condition)) {
39+
return call_user_func($this->condition) ? 'prohibited' : '';
40+
}
41+
42+
return $this->condition ? 'prohibited' : '';
43+
}
44+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Validation;
4+
5+
use Exception;
6+
use Illuminate\Validation\Rules\ExcludeIf;
7+
use InvalidArgumentException;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ValidationExcludeIfTest extends TestCase
11+
{
12+
public function testItClousureReturnsFormatsAStringVersionOfTheRule()
13+
{
14+
$rule = new ExcludeIf(function () {
15+
return true;
16+
});
17+
18+
$this->assertSame('exclude', (string) $rule);
19+
20+
$rule = new ExcludeIf(function () {
21+
return false;
22+
});
23+
24+
$this->assertSame('', (string) $rule);
25+
26+
$rule = new ExcludeIf(true);
27+
28+
$this->assertSame('exclude', (string) $rule);
29+
30+
$rule = new ExcludeIf(false);
31+
32+
$this->assertSame('', (string) $rule);
33+
}
34+
35+
public function testItOnlyCallableAndBooleanAreAcceptableArgumentsOfTheRule()
36+
{
37+
$rule = new ExcludeIf(false);
38+
39+
$rule = new ExcludeIf(true);
40+
41+
$this->expectException(InvalidArgumentException::class);
42+
43+
$rule = new ExcludeIf('phpinfo');
44+
}
45+
46+
public function testItReturnedRuleIsNotSerializable()
47+
{
48+
$this->expectException(Exception::class);
49+
50+
$rule = serialize(new ExcludeIf(function () {
51+
return true;
52+
}));
53+
}
54+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
<?php
2+
3+
namespace Illuminate\Tests\Validation;
4+
5+
use Exception;
6+
use Illuminate\Validation\Rules\ProhibitedIf;
7+
use InvalidArgumentException;
8+
use PHPUnit\Framework\TestCase;
9+
10+
class ValidationProhibitedIfTest extends TestCase
11+
{
12+
public function testItClousureReturnsFormatsAStringVersionOfTheRule()
13+
{
14+
$rule = new ProhibitedIf(function () {
15+
return true;
16+
});
17+
18+
$this->assertSame('prohibited', (string) $rule);
19+
20+
$rule = new ProhibitedIf(function () {
21+
return false;
22+
});
23+
24+
$this->assertSame('', (string) $rule);
25+
26+
$rule = new ProhibitedIf(true);
27+
28+
$this->assertSame('prohibited', (string) $rule);
29+
30+
$rule = new ProhibitedIf(false);
31+
32+
$this->assertSame('', (string) $rule);
33+
}
34+
35+
public function testItOnlyCallableAndBooleanAreAcceptableArgumentsOfTheRule()
36+
{
37+
$rule = new ProhibitedIf(false);
38+
39+
$rule = new ProhibitedIf(true);
40+
41+
$this->expectException(InvalidArgumentException::class);
42+
43+
$rule = new ProhibitedIf('phpinfo');
44+
}
45+
46+
public function testItReturnedRuleIsNotSerializable()
47+
{
48+
$this->expectException(Exception::class);
49+
50+
$rule = serialize(new ProhibitedIf(function () {
51+
return true;
52+
}));
53+
}
54+
}

0 commit comments

Comments
 (0)