Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] Add prohibited validation rule #36667

Merged
merged 1 commit into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
* @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