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

[6.x] Add exclude_if validation rule #30835

Merged
merged 7 commits into from Dec 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 52 additions & 5 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Expand Up @@ -1396,6 +1396,57 @@ public function validateRequiredIf($attribute, $value, $parameters)
{
$this->requireParameterCount(2, $parameters, 'required_if');

[$values, $other] = $this->prepareValuesAndOther($parameters);

if (in_array($other, $values)) {
return $this->validateRequired($attribute, $value);
}

return true;
}

/**
* Indicate that an attribute should be excluded when another attribute has a given value.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
public function validateExcludeIf($attribute, $value, $parameters)
{
$this->requireParameterCount(2, $parameters, 'exclude_if');

[$values, $other] = $this->prepareValuesAndOther($parameters);

return ! in_array($other, $values);
}

/**
* Indicate that an attribute should be excluded when another attribute does not have a given value.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
public function validateExcludeUnless($attribute, $value, $parameters)
{
$this->requireParameterCount(2, $parameters, 'exclude_unless');

[$values, $other] = $this->prepareValuesAndOther($parameters);

return in_array($other, $values);
}

/**
* Prepare the values and the other value for validation.
*
* @param array $parameters
* @return array
*/
protected function prepareValuesAndOther($parameters)
{
$other = Arr::get($this->data, $parameters[0]);

$values = array_slice($parameters, 1);
Expand All @@ -1404,11 +1455,7 @@ public function validateRequiredIf($attribute, $value, $parameters)
$values = $this->convertValuesToBoolean($values);
}

if (in_array($other, $values)) {
return $this->validateRequired($attribute, $value);
}

return true;
return [$values, $other];
}

/**
Expand Down
72 changes: 72 additions & 0 deletions src/Illuminate/Validation/Validator.php
Expand Up @@ -48,6 +48,13 @@ class Validator implements ValidatorContract
*/
protected $failedRules = [];

/**
* Attributes that should be excluded from the validated data.
*
* @var array
*/
protected $excludeAttributes = [];

/**
* The message bag instance.
*
Expand Down Expand Up @@ -177,6 +184,13 @@ class Validator implements ValidatorContract
'Before', 'After', 'BeforeOrEqual', 'AfterOrEqual', 'Gt', 'Lt', 'Gte', 'Lte',
];

/**
* The validation rules that can exclude an attribute.
*
* @var array
*/
protected $excludeRules = ['ExcludeIf', 'ExcludeUnless'];

/**
* The size related validation rules.
*
Expand Down Expand Up @@ -273,9 +287,23 @@ public function passes()
foreach ($this->rules as $attribute => $rules) {
$attribute = str_replace('\.', '->', $attribute);

// If this attribute is a nested rule, its parent might have already
// been excluded. If so, we have to remove the attribute.
if ($this->shouldBeExcluded($attribute)) {
$this->removeAttribute($attribute);

continue;
}

foreach ($rules as $rule) {
$this->validateAttribute($attribute, $rule);

if ($this->shouldBeExcluded($attribute)) {
$this->removeAttribute($attribute);

break;
}

if ($this->shouldStopValidating($attribute)) {
break;
}
Expand All @@ -292,6 +320,40 @@ public function passes()
return $this->messages->isEmpty();
}

/**
* Determine if the attribute should be excluded.
*
* @param string $attribute
*
* @return bool
*/
protected function shouldBeExcluded($attribute)
{
foreach ($this->excludeAttributes as $excludeAttribute) {
if ($attribute === $excludeAttribute) {
return true;
}

if (Str::startsWith($attribute, $excludeAttribute.'.')) {
return true;
}
}

return false;
}

/**
* Remove the given attribute.
*
* @param string $attribute
*
* @return void
*/
protected function removeAttribute($attribute)
{
unset($this->data[$attribute], $this->rules[$attribute]);
}

/**
* Determine if the data fails the validation rules.
*
Expand Down Expand Up @@ -475,6 +537,10 @@ protected function replaceAsterisksInParameters(array $parameters, array $keys)
*/
protected function isValidatable($rule, $attribute, $value)
{
if (in_array($rule, $this->excludeRules)) {
return true;
}

return $this->presentOrRuleIsImplicit($rule, $attribute, $value) &&
$this->passesOptionalCheck($attribute) &&
$this->isNotNullIfMarkedAsNullable($rule, $attribute) &&
Expand Down Expand Up @@ -621,6 +687,12 @@ public function addFailure($attribute, $rule, $parameters = [])
$this->passes();
}

if (in_array($rule, $this->excludeRules)) {
$this->excludeAttributes[] = $attribute;

return;
}

$this->messages->add($attribute, $this->makeReplacements(
$this->getMessage($attribute, $rule), $attribute, $rule, $parameters
));
Expand Down