Skip to content

Commit 71728e3

Browse files
authored
Added validation rule prohibiti. (#6885)
1 parent 0a989af commit 71728e3

File tree

6 files changed

+80
-0
lines changed

6 files changed

+80
-0
lines changed

publish/en/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
'not_regex' => 'The :attribute cannot match a given regular rule.',
103103
'numeric' => 'The :attribute must be a number.',
104104
'present' => 'The :attribute field must be present.',
105+
'prohibits' => 'The :attribute field must be present.',
105106
'regex' => 'The :attribute format is invalid.',
106107
'required' => 'The :attribute field is required.',
107108
'required_if' => 'The :attribute field is required when :other is :value.',

publish/zh_CN/validation.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@
102102
'not_regex' => ':attribute 不能匹配给定的正则',
103103
'numeric' => ':attribute 必须是数字',
104104
'present' => ':attribute 字段必须存在',
105+
'prohibits' => '必须提供 :attribute 字段',
105106
'regex' => ':attribute 格式是无效的',
106107
'required' => ':attribute 字段是必须的',
107108
'required_if' => ':attribute 字段是必须的当 :other 是 :value',

src/Concerns/ReplacesAttributes.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,13 @@ protected function replaceStartsWith(string $message, string $attribute, string
337337

338338
return str_replace(':values', implode(', ', $parameters), $message);
339339
}
340+
341+
/**
342+
* Replace all place-holders for the prohibited_with rule.
343+
* @param array<int,string> $parameters
344+
*/
345+
protected function replaceProhibits(string $message, string $attribute, string $rule, array $parameters): string
346+
{
347+
return str_replace(':other', implode(' / ', $this->getAttributeList($parameters)), $message);
348+
}
340349
}

src/Concerns/ValidatesAttributes.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,22 @@ public function validateRequired(string $attribute, $value): bool
12571257
return true;
12581258
}
12591259

1260+
/**
1261+
* Validate that other attributes do not exist when this attribute exists.
1262+
*/
1263+
public function validateProhibits(string $attribute, mixed $value, mixed $parameters): bool
1264+
{
1265+
if ($this->validateRequired($attribute, $value)) {
1266+
foreach ($parameters as $parameter) {
1267+
if ($this->validateRequired($parameter, Arr::get($this->data, $parameter))) {
1268+
return false;
1269+
}
1270+
}
1271+
}
1272+
1273+
return true;
1274+
}
1275+
12601276
/**
12611277
* Validate that an attribute exists when another attribute has a given value.
12621278
*

src/Validator.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class Validator implements ValidatorContract
137137
'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll',
138138
'RequiredIf', 'RequiredUnless', 'Confirmed', 'Same', 'Different', 'Unique',
139139
'Before', 'After', 'BeforeOrEqual', 'AfterOrEqual', 'Gt', 'Lt', 'Gte', 'Lte',
140+
'Prohibits',
140141
];
141142

142143
/**

tests/Cases/ValidationValidatorTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4888,6 +4888,58 @@ public function testInputIsReplacedByItsDisplayableValue()
48884888
$this->assertSame('Rails is not a valid PHP Framework', $v->messages()->first('framework'));
48894889
}
48904890

4891+
public function testProhibits()
4892+
{
4893+
$trans = $this->getIlluminateArrayTranslator();
4894+
$v = new Validator($trans, ['email' => 'foo', 'emails' => ['foo']], ['email' => 'prohibits:emails']);
4895+
$this->assertTrue($v->fails());
4896+
4897+
$trans = $this->getIlluminateArrayTranslator();
4898+
$v = new Validator($trans, ['email' => 'foo', 'emails' => []], ['email' => 'prohibits:emails']);
4899+
$this->assertTrue($v->passes());
4900+
4901+
$trans = $this->getIlluminateArrayTranslator();
4902+
$v = new Validator($trans, ['email' => 'foo', 'emails' => ''], ['email' => 'prohibits:emails']);
4903+
$this->assertTrue($v->passes());
4904+
4905+
$trans = $this->getIlluminateArrayTranslator();
4906+
$v = new Validator($trans, ['email' => 'foo', 'emails' => null], ['email' => 'prohibits:emails']);
4907+
$this->assertTrue($v->passes());
4908+
4909+
$trans = $this->getIlluminateArrayTranslator();
4910+
$v = new Validator($trans, ['email' => 'foo', 'emails' => false], ['email' => 'prohibits:emails']);
4911+
$this->assertTrue($v->fails());
4912+
4913+
$trans = $this->getIlluminateArrayTranslator();
4914+
$v = new Validator($trans, ['email' => 'foo', 'emails' => ['foo']], ['email' => 'prohibits:email_address,emails']);
4915+
$this->assertTrue($v->fails());
4916+
4917+
$trans = $this->getIlluminateArrayTranslator();
4918+
$v = new Validator($trans, ['email' => 'foo'], ['email' => 'prohibits:emails']);
4919+
$this->assertTrue($v->passes());
4920+
4921+
$trans = $this->getIlluminateArrayTranslator();
4922+
$v = new Validator($trans, ['email' => 'foo', 'other' => 'foo'], ['email' => 'prohibits:email_address,emails']);
4923+
$this->assertTrue($v->passes());
4924+
4925+
$trans = $this->getIlluminateArrayTranslator();
4926+
$trans->addLines(['validation.prohibits' => 'The :attribute field prohibits :other being present.'], 'en');
4927+
$v = new Validator($trans, ['email' => 'foo', 'emails' => 'bar', 'email_address' => 'baz'], ['email' => 'prohibits:emails,email_address']);
4928+
$this->assertFalse($v->passes());
4929+
$this->assertSame('The email field prohibits emails / email address being present.', $v->messages()->first('email'));
4930+
4931+
$trans = $this->getIlluminateArrayTranslator();
4932+
$v = new Validator($trans, [
4933+
'foo' => [
4934+
['email' => 'foo', 'emails' => 'foo'],
4935+
['emails' => 'foo'],
4936+
],
4937+
], ['foo.*.email' => 'prohibits:foo.*.emails']);
4938+
$this->assertFalse($v->passes());
4939+
$this->assertTrue($v->messages()->has('foo.0.email'));
4940+
$this->assertFalse($v->messages()->has('foo.1.email'));
4941+
}
4942+
48914943
public function getIlluminateArrayTranslator()
48924944
{
48934945
return new Translator(

0 commit comments

Comments
 (0)