Skip to content

Commit 0de4d79

Browse files
authored
Add prohibited validation rule (#36667)
1 parent a64526c commit 0de4d79

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1434,6 +1434,19 @@ public function validateRequiredIf($attribute, $value, $parameters)
14341434
return true;
14351435
}
14361436

1437+
/**
1438+
* Validate that an attribute does not exist.
1439+
*
1440+
* @param string $attribute
1441+
* @param mixed $value
1442+
* @param mixed $parameters
1443+
* @return bool
1444+
*/
1445+
public function validateProhibited($attribute, $value)
1446+
{
1447+
return false;
1448+
}
1449+
14371450
/**
14381451
* Validate that an attribute does not exist when another attribute has a given value.
14391452
*

src/Illuminate/Validation/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,7 @@ class Validator implements ValidatorContract
227227
'RequiredWithAll',
228228
'RequiredWithout',
229229
'RequiredWithoutAll',
230+
'Prohibited',
230231
'ProhibitedIf',
231232
'ProhibitedUnless',
232233
'Same',

tests/Validation/ValidationValidatorTest.php

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1190,6 +1190,36 @@ public function testRequiredUnless()
11901190
$this->assertSame('The last field is required unless first is in taylor, sven.', $v->messages()->first('last'));
11911191
}
11921192

1193+
public function testProhibited()
1194+
{
1195+
$trans = $this->getIlluminateArrayTranslator();
1196+
1197+
$v = new Validator($trans, [], ['name' => 'prohibited']);
1198+
$this->assertTrue($v->passes());
1199+
1200+
$v = new Validator($trans, ['last' => 'bar'], ['name' => 'prohibited']);
1201+
$this->assertTrue($v->passes());
1202+
1203+
$v = new Validator($trans, ['name' => 'foo'], ['name' => 'prohibited']);
1204+
$this->assertTrue($v->fails());
1205+
1206+
$file = new File('', false);
1207+
$v = new Validator($trans, ['name' => $file], ['name' => 'prohibited']);
1208+
$this->assertTrue($v->fails());
1209+
1210+
$file = new File(__FILE__, false);
1211+
$v = new Validator($trans, ['name' => $file], ['name' => 'prohibited']);
1212+
$this->assertTrue($v->fails());
1213+
1214+
$file = new File(__FILE__, false);
1215+
$file2 = new File(__FILE__, false);
1216+
$v = new Validator($trans, ['files' => [$file, $file2]], ['files.0' => 'prohibited', 'files.1' => 'prohibited']);
1217+
$this->assertTrue($v->fails());
1218+
1219+
$v = new Validator($trans, ['files' => [$file, $file2]], ['files' => 'prohibited']);
1220+
$this->assertTrue($v->fails());
1221+
}
1222+
11931223
public function testProhibitedIf()
11941224
{
11951225
$trans = $this->getIlluminateArrayTranslator();

0 commit comments

Comments
 (0)