Skip to content

Commit

Permalink
Add prohibited validation rule (#36667)
Browse files Browse the repository at this point in the history
  • Loading branch information
PhiloNL authored Mar 19, 2021
1 parent a64526c commit 0de4d79
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
13 changes: 13 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1434,6 +1434,19 @@ public function validateRequiredIf($attribute, $value, $parameters)
return true;
}

/**
* Validate that an attribute does not exist.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters

This comment has been minimized.

Copy link
@selcukcukur

selcukcukur Mar 19, 2021

Contributor

This parameter does not exist.

* @return bool
*/
public function validateProhibited($attribute, $value)
{
return false;
}

/**
* Validate that an attribute does not exist when another attribute has a given value.
*
Expand Down
1 change: 1 addition & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class Validator implements ValidatorContract
'RequiredWithAll',
'RequiredWithout',
'RequiredWithoutAll',
'Prohibited',
'ProhibitedIf',
'ProhibitedUnless',
'Same',
Expand Down
30 changes: 30 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1190,6 +1190,36 @@ public function testRequiredUnless()
$this->assertSame('The last field is required unless first is in taylor, sven.', $v->messages()->first('last'));
}

public function testProhibited()
{
$trans = $this->getIlluminateArrayTranslator();

$v = new Validator($trans, [], ['name' => 'prohibited']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['last' => 'bar'], ['name' => 'prohibited']);
$this->assertTrue($v->passes());

$v = new Validator($trans, ['name' => 'foo'], ['name' => 'prohibited']);
$this->assertTrue($v->fails());

$file = new File('', false);
$v = new Validator($trans, ['name' => $file], ['name' => 'prohibited']);
$this->assertTrue($v->fails());

$file = new File(__FILE__, false);
$v = new Validator($trans, ['name' => $file], ['name' => 'prohibited']);
$this->assertTrue($v->fails());

$file = new File(__FILE__, false);
$file2 = new File(__FILE__, false);
$v = new Validator($trans, ['files' => [$file, $file2]], ['files.0' => 'prohibited', 'files.1' => 'prohibited']);
$this->assertTrue($v->fails());

$v = new Validator($trans, ['files' => [$file, $file2]], ['files' => 'prohibited']);
$this->assertTrue($v->fails());
}

public function testProhibitedIf()
{
$trans = $this->getIlluminateArrayTranslator();
Expand Down

0 comments on commit 0de4d79

Please sign in to comment.