Skip to content

Commit

Permalink
Added validation rule prohibiti. (#6885)
Browse files Browse the repository at this point in the history
  • Loading branch information
zds-s committed Jun 24, 2024
1 parent 0a989af commit 71728e3
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 0 deletions.
1 change: 1 addition & 0 deletions publish/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
'not_regex' => 'The :attribute cannot match a given regular rule.',
'numeric' => 'The :attribute must be a number.',
'present' => 'The :attribute field must be present.',
'prohibits' => 'The :attribute field must be present.',
'regex' => 'The :attribute format is invalid.',
'required' => 'The :attribute field is required.',
'required_if' => 'The :attribute field is required when :other is :value.',
Expand Down
1 change: 1 addition & 0 deletions publish/zh_CN/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@
'not_regex' => ':attribute 不能匹配给定的正则',
'numeric' => ':attribute 必须是数字',
'present' => ':attribute 字段必须存在',
'prohibits' => '必须提供 :attribute 字段',
'regex' => ':attribute 格式是无效的',
'required' => ':attribute 字段是必须的',
'required_if' => ':attribute 字段是必须的当 :other 是 :value',
Expand Down
9 changes: 9 additions & 0 deletions src/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -337,4 +337,13 @@ protected function replaceStartsWith(string $message, string $attribute, string

return str_replace(':values', implode(', ', $parameters), $message);
}

/**
* Replace all place-holders for the prohibited_with rule.
* @param array<int,string> $parameters
*/
protected function replaceProhibits(string $message, string $attribute, string $rule, array $parameters): string
{
return str_replace(':other', implode(' / ', $this->getAttributeList($parameters)), $message);
}
}
16 changes: 16 additions & 0 deletions src/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,22 @@ public function validateRequired(string $attribute, $value): bool
return true;
}

/**
* Validate that other attributes do not exist when this attribute exists.
*/
public function validateProhibits(string $attribute, mixed $value, mixed $parameters): bool
{
if ($this->validateRequired($attribute, $value)) {
foreach ($parameters as $parameter) {
if ($this->validateRequired($parameter, Arr::get($this->data, $parameter))) {
return false;
}
}
}

return true;
}

/**
* Validate that an attribute exists when another attribute has a given value.
*
Expand Down
1 change: 1 addition & 0 deletions src/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class Validator implements ValidatorContract
'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll',
'RequiredIf', 'RequiredUnless', 'Confirmed', 'Same', 'Different', 'Unique',
'Before', 'After', 'BeforeOrEqual', 'AfterOrEqual', 'Gt', 'Lt', 'Gte', 'Lte',
'Prohibits',
];

/**
Expand Down
52 changes: 52 additions & 0 deletions tests/Cases/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4888,6 +4888,58 @@ public function testInputIsReplacedByItsDisplayableValue()
$this->assertSame('Rails is not a valid PHP Framework', $v->messages()->first('framework'));
}

public function testProhibits()
{
$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['email' => 'foo', 'emails' => ['foo']], ['email' => 'prohibits:emails']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['email' => 'foo', 'emails' => []], ['email' => 'prohibits:emails']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['email' => 'foo', 'emails' => ''], ['email' => 'prohibits:emails']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['email' => 'foo', 'emails' => null], ['email' => 'prohibits:emails']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['email' => 'foo', 'emails' => false], ['email' => 'prohibits:emails']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['email' => 'foo', 'emails' => ['foo']], ['email' => 'prohibits:email_address,emails']);
$this->assertTrue($v->fails());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['email' => 'foo'], ['email' => 'prohibits:emails']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, ['email' => 'foo', 'other' => 'foo'], ['email' => 'prohibits:email_address,emails']);
$this->assertTrue($v->passes());

$trans = $this->getIlluminateArrayTranslator();
$trans->addLines(['validation.prohibits' => 'The :attribute field prohibits :other being present.'], 'en');
$v = new Validator($trans, ['email' => 'foo', 'emails' => 'bar', 'email_address' => 'baz'], ['email' => 'prohibits:emails,email_address']);
$this->assertFalse($v->passes());
$this->assertSame('The email field prohibits emails / email address being present.', $v->messages()->first('email'));

$trans = $this->getIlluminateArrayTranslator();
$v = new Validator($trans, [
'foo' => [
['email' => 'foo', 'emails' => 'foo'],
['emails' => 'foo'],
],
], ['foo.*.email' => 'prohibits:foo.*.emails']);
$this->assertFalse($v->passes());
$this->assertTrue($v->messages()->has('foo.0.email'));
$this->assertFalse($v->messages()->has('foo.1.email'));
}

public function getIlluminateArrayTranslator()
{
return new Translator(
Expand Down

0 comments on commit 71728e3

Please sign in to comment.