From 67fcc272752effeed98889d4c8a506d783f1cde4 Mon Sep 17 00:00:00 2001 From: Osama Sadah Date: Sun, 2 Feb 2025 10:00:18 +0300 Subject: [PATCH] feat: add a prohibited_if_declined and prohibited_if_accepted validation rules --- .../Translation/lang/en/validation.php | 2 + .../Concerns/ReplacesAttributes.php | 32 ++++++++++++++++ .../Concerns/ValidatesAttributes.php | 38 +++++++++++++++++++ src/Illuminate/Validation/Validator.php | 2 + 4 files changed, 74 insertions(+) diff --git a/src/Illuminate/Translation/lang/en/validation.php b/src/Illuminate/Translation/lang/en/validation.php index dddc94782ad9..71bb69c3d119 100644 --- a/src/Illuminate/Translation/lang/en/validation.php +++ b/src/Illuminate/Translation/lang/en/validation.php @@ -131,6 +131,8 @@ 'present_with_all' => 'The :attribute field must be present when :values are present.', 'prohibited' => 'The :attribute field is prohibited.', 'prohibited_if' => 'The :attribute field is prohibited when :other is :value.', + 'prohibited_if_declined' => 'The :attribute field is prohibited when :other is accepted.', + 'prohibited_if_accepted' => 'The :attribute field is prohibited when :other is declined.', 'prohibited_unless' => 'The :attribute field is prohibited unless :other is in :values.', 'prohibits' => 'The :attribute field prohibits :other from being present.', 'regex' => 'The :attribute field format is invalid.', diff --git a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php index 5d420df14029..202398f33195 100644 --- a/src/Illuminate/Validation/Concerns/ReplacesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ReplacesAttributes.php @@ -665,6 +665,38 @@ protected function replaceProhibitedIf($message, $attribute, $rule, $parameters) return str_replace([':other', ':value'], $parameters, $message); } + /** + * Replace all place-holders for the prohibited_if_accepted rule. + * + * @param string $message + * @param string $attribute + * @param string $rule + * @param array $parameters + * @return string + */ + protected function replaceProhibitedIfAccepted($message, $attribute, $rule, $parameters) + { + $parameters[0] = $this->getDisplayableAttribute($parameters[0]); + + return str_replace([':other'], $parameters, $message); + } + + /** + * Replace all place-holders for the prohibited_if_declined rule. + * + * @param string $message + * @param string $attribute + * @param string $rule + * @param array $parameters + * @return string + */ + public function replaceProhibitedIfDeclined($message, $attribute, $rule, $parameters) + { + $parameters[0] = $this->getDisplayableAttribute($parameters[0]); + + return str_replace([':other'], $parameters, $message); + } + /** * Replace all place-holders for the prohibited_unless rule. * diff --git a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php index 223181af9766..a2dfb054a7f6 100644 --- a/src/Illuminate/Validation/Concerns/ValidatesAttributes.php +++ b/src/Illuminate/Validation/Concerns/ValidatesAttributes.php @@ -2082,6 +2082,44 @@ public function validateProhibitedIf($attribute, $value, $parameters) return true; } + /** + * Validate that an attribute does not exist when another attribute was "accepted". + * + * @param string $attribute + * @param mixed $value + * @param mixed $parameters + * @return bool + */ + public function validateProhibitedIfAccepted($attribute, $value, $parameters) + { + $this->requireParameterCount(1, $parameters, 'prohibited_if_accepted'); + + if ($this->validateAccepted($parameters[0], $this->getValue($parameters[0]))) { + return $this->validateProhibited($attribute, $value); + } + + return true; + } + + /** + * Validate that an attribute does not exist when another attribute was "declined". + * + * @param string $attribute + * @param mixed $value + * @param mixed $parameters + * @return bool + */ + public function validateProhibitedIfDeclined($attribute, $value, $parameters) + { + $this->requireParameterCount(1, $parameters, 'prohibited_if_declined'); + + if ($this->validateDeclined($parameters[0], $this->getValue($parameters[0]))) { + return $this->validateProhibited($attribute, $value); + } + + return true; + } + /** * Validate that an attribute does not exist unless another attribute has a given value. * diff --git a/src/Illuminate/Validation/Validator.php b/src/Illuminate/Validation/Validator.php index cdba5179f5c2..557ab19f57a2 100755 --- a/src/Illuminate/Validation/Validator.php +++ b/src/Illuminate/Validation/Validator.php @@ -266,6 +266,8 @@ class Validator implements ValidatorContract 'PresentWithAll', 'Prohibited', 'ProhibitedIf', + 'ProhibitedIfAccepted', + 'ProhibitedIfDeclined', 'ProhibitedUnless', 'Prohibits', 'MissingIf',