From 71728e301637482c809c3ef7e73955cddd7c6431 Mon Sep 17 00:00:00 2001 From: zds <49744633+zds-s@users.noreply.github.com> Date: Mon, 24 Jun 2024 22:16:23 +0800 Subject: [PATCH] Added validation rule `prohibiti`. (#6885) --- publish/en/validation.php | 1 + publish/zh_CN/validation.php | 1 + src/Concerns/ReplacesAttributes.php | 9 +++++ src/Concerns/ValidatesAttributes.php | 16 ++++++++ src/Validator.php | 1 + tests/Cases/ValidationValidatorTest.php | 52 +++++++++++++++++++++++++ 6 files changed, 80 insertions(+) diff --git a/publish/en/validation.php b/publish/en/validation.php index c911d4a..a7f050e 100644 --- a/publish/en/validation.php +++ b/publish/en/validation.php @@ -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.', diff --git a/publish/zh_CN/validation.php b/publish/zh_CN/validation.php index 8821b0b..877edd1 100644 --- a/publish/zh_CN/validation.php +++ b/publish/zh_CN/validation.php @@ -102,6 +102,7 @@ 'not_regex' => ':attribute 不能匹配给定的正则', 'numeric' => ':attribute 必须是数字', 'present' => ':attribute 字段必须存在', + 'prohibits' => '必须提供 :attribute 字段', 'regex' => ':attribute 格式是无效的', 'required' => ':attribute 字段是必须的', 'required_if' => ':attribute 字段是必须的当 :other 是 :value', diff --git a/src/Concerns/ReplacesAttributes.php b/src/Concerns/ReplacesAttributes.php index 6ad1e91..607aa33 100755 --- a/src/Concerns/ReplacesAttributes.php +++ b/src/Concerns/ReplacesAttributes.php @@ -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 $parameters + */ + protected function replaceProhibits(string $message, string $attribute, string $rule, array $parameters): string + { + return str_replace(':other', implode(' / ', $this->getAttributeList($parameters)), $message); + } } diff --git a/src/Concerns/ValidatesAttributes.php b/src/Concerns/ValidatesAttributes.php index e9cfb39..78b0d28 100755 --- a/src/Concerns/ValidatesAttributes.php +++ b/src/Concerns/ValidatesAttributes.php @@ -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. * diff --git a/src/Validator.php b/src/Validator.php index 9cdadfb..5374148 100755 --- a/src/Validator.php +++ b/src/Validator.php @@ -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', ]; /** diff --git a/tests/Cases/ValidationValidatorTest.php b/tests/Cases/ValidationValidatorTest.php index 8449780..16ccc15 100755 --- a/tests/Cases/ValidationValidatorTest.php +++ b/tests/Cases/ValidationValidatorTest.php @@ -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(