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

[11.x] Minor Feature a Prohibited If Declined and Prohibited If Accepted validation rules #54439

Open
wants to merge 1 commit into
base: 11.x
Choose a base branch
from
Open
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
2 changes: 2 additions & 0 deletions src/Illuminate/Translation/lang/en/validation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.',
Expand Down
32 changes: 32 additions & 0 deletions src/Illuminate/Validation/Concerns/ReplacesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<int,string> $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<int,string> $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.
*
Expand Down
38 changes: 38 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ class Validator implements ValidatorContract
'PresentWithAll',
'Prohibited',
'ProhibitedIf',
'ProhibitedIfAccepted',
'ProhibitedIfDeclined',
'ProhibitedUnless',
'Prohibits',
'MissingIf',
Expand Down