Skip to content

Commit

Permalink
Add exclude_without rule.
Browse files Browse the repository at this point in the history
This adds the ability to exclude an attribute from validation failure if another field is not present.
  • Loading branch information
taylorotwell committed Apr 23, 2020
1 parent aaf768e commit 4083ae5
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
19 changes: 19 additions & 0 deletions src/Illuminate/Validation/Concerns/ValidatesAttributes.php
Original file line number Diff line number Diff line change
Expand Up @@ -1444,6 +1444,25 @@ public function validateExcludeUnless($attribute, $value, $parameters)
return in_array($other, $values);
}

/**
* Indicate that an attribute should be excluded when another attribute is missing.
*
* @param string $attribute
* @param mixed $value
* @param mixed $parameters
* @return bool
*/
public function validateExcludeWithout($attribute, $value, $parameters)
{
$this->requireParameterCount(1, $parameters, 'exclude_without');

if ($this->anyFailingRequired($parameters)) {
return false;
}

return true;
}

/**
* Prepare the values and the other value for validation.
*
Expand Down
50 changes: 41 additions & 9 deletions src/Illuminate/Validation/Validator.php
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,15 @@ class Validator implements ValidatorContract
* @var array
*/
protected $fileRules = [
'File', 'Image', 'Mimes', 'Mimetypes', 'Min',
'Max', 'Size', 'Between', 'Dimensions',
'Between',
'Dimensions',
'File',
'Image',
'Max',
'Mimes',
'Mimetypes',
'Min',
'Size',
];

/**
Expand All @@ -176,8 +183,16 @@ class Validator implements ValidatorContract
* @var array
*/
protected $implicitRules = [
'Required', 'Filled', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout',
'RequiredWithoutAll', 'RequiredIf', 'RequiredUnless', 'Accepted', 'Present',
'Accepted',
'Filled',
'Present',
'Required',
'RequiredIf',
'RequiredUnless',
'RequiredWith',
'RequiredWithAll',
'RequiredWithout',
'RequiredWithoutAll',
];

/**
Expand All @@ -186,18 +201,35 @@ class Validator implements ValidatorContract
* @var array
*/
protected $dependentRules = [
'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll',
'RequiredIf', 'RequiredUnless', 'Confirmed', 'Same', 'Different', 'Unique',
'Before', 'After', 'BeforeOrEqual', 'AfterOrEqual', 'Gt', 'Lt', 'Gte', 'Lte',
'ExcludeIf', 'ExcludeUnless',
'After',
'AfterOrEqual',
'Before',
'BeforeOrEqual',
'Confirmed',
'Different',
'ExcludeIf',
'ExcludeUnless',
'ExcludeWithout',
'Gt',
'Gte',
'Lt',
'Lte',
'RequiredIf',
'RequiredUnless',
'RequiredWith',
'RequiredWithAll',
'RequiredWithout',
'RequiredWithoutAll',
'Same',
'Unique',
];

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

/**
* The size related validation rules.
Expand Down
15 changes: 15 additions & 0 deletions tests/Validation/ValidationValidatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5204,6 +5204,21 @@ public function testExcludeUnless()
$this->assertSame(['mouse' => ['validation.required']], $validator->messages()->toArray());
}

public function testExcludeWithout()
{
$validator = new Validator(
$this->getIlluminateArrayTranslator(),
['region' => 'South'],
[
'country' => 'exclude_without:region|nullable|required_with:region|string|min:3',
'region' => 'exclude_without:country|nullable|required_with:country|string|min:3',
]
);

$this->assertTrue($validator->fails());
$this->assertSame(['country' => ['validation.required_with']], $validator->messages()->toArray());
}

public function testExcludeValuesAreReallyRemoved()
{
$validator = new Validator(
Expand Down

0 comments on commit 4083ae5

Please sign in to comment.