Skip to content

Commit

Permalink
Merge branch '6.x' of https://github.com/SjorsO/framework into SjorsO…
Browse files Browse the repository at this point in the history
…-6.x
  • Loading branch information
taylorotwell committed Dec 27, 2019
2 parents 5cf26ec + f9ea321 commit 4703e39
Show file tree
Hide file tree
Showing 3 changed files with 407 additions and 5 deletions.
57 changes: 52 additions & 5 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1392,6 +1392,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 @@ -1400,11 +1451,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
Original file line number Diff line number Diff line change
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
Loading

0 comments on commit 4703e39

Please sign in to comment.