Skip to content

Commit 4083ae5

Browse files
committed
Add exclude_without rule.
This adds the ability to exclude an attribute from validation failure if another field is not present.
1 parent aaf768e commit 4083ae5

File tree

3 files changed

+75
-9
lines changed

3 files changed

+75
-9
lines changed

src/Illuminate/Validation/Concerns/ValidatesAttributes.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,25 @@ public function validateExcludeUnless($attribute, $value, $parameters)
14441444
return in_array($other, $values);
14451445
}
14461446

1447+
/**
1448+
* Indicate that an attribute should be excluded when another attribute is missing.
1449+
*
1450+
* @param string $attribute
1451+
* @param mixed $value
1452+
* @param mixed $parameters
1453+
* @return bool
1454+
*/
1455+
public function validateExcludeWithout($attribute, $value, $parameters)
1456+
{
1457+
$this->requireParameterCount(1, $parameters, 'exclude_without');
1458+
1459+
if ($this->anyFailingRequired($parameters)) {
1460+
return false;
1461+
}
1462+
1463+
return true;
1464+
}
1465+
14471466
/**
14481467
* Prepare the values and the other value for validation.
14491468
*

src/Illuminate/Validation/Validator.php

Lines changed: 41 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,15 @@ class Validator implements ValidatorContract
166166
* @var array
167167
*/
168168
protected $fileRules = [
169-
'File', 'Image', 'Mimes', 'Mimetypes', 'Min',
170-
'Max', 'Size', 'Between', 'Dimensions',
169+
'Between',
170+
'Dimensions',
171+
'File',
172+
'Image',
173+
'Max',
174+
'Mimes',
175+
'Mimetypes',
176+
'Min',
177+
'Size',
171178
];
172179

173180
/**
@@ -176,8 +183,16 @@ class Validator implements ValidatorContract
176183
* @var array
177184
*/
178185
protected $implicitRules = [
179-
'Required', 'Filled', 'RequiredWith', 'RequiredWithAll', 'RequiredWithout',
180-
'RequiredWithoutAll', 'RequiredIf', 'RequiredUnless', 'Accepted', 'Present',
186+
'Accepted',
187+
'Filled',
188+
'Present',
189+
'Required',
190+
'RequiredIf',
191+
'RequiredUnless',
192+
'RequiredWith',
193+
'RequiredWithAll',
194+
'RequiredWithout',
195+
'RequiredWithoutAll',
181196
];
182197

183198
/**
@@ -186,18 +201,35 @@ class Validator implements ValidatorContract
186201
* @var array
187202
*/
188203
protected $dependentRules = [
189-
'RequiredWith', 'RequiredWithAll', 'RequiredWithout', 'RequiredWithoutAll',
190-
'RequiredIf', 'RequiredUnless', 'Confirmed', 'Same', 'Different', 'Unique',
191-
'Before', 'After', 'BeforeOrEqual', 'AfterOrEqual', 'Gt', 'Lt', 'Gte', 'Lte',
192-
'ExcludeIf', 'ExcludeUnless',
204+
'After',
205+
'AfterOrEqual',
206+
'Before',
207+
'BeforeOrEqual',
208+
'Confirmed',
209+
'Different',
210+
'ExcludeIf',
211+
'ExcludeUnless',
212+
'ExcludeWithout',
213+
'Gt',
214+
'Gte',
215+
'Lt',
216+
'Lte',
217+
'RequiredIf',
218+
'RequiredUnless',
219+
'RequiredWith',
220+
'RequiredWithAll',
221+
'RequiredWithout',
222+
'RequiredWithoutAll',
223+
'Same',
224+
'Unique',
193225
];
194226

195227
/**
196228
* The validation rules that can exclude an attribute.
197229
*
198230
* @var array
199231
*/
200-
protected $excludeRules = ['ExcludeIf', 'ExcludeUnless'];
232+
protected $excludeRules = ['ExcludeIf', 'ExcludeUnless', 'ExcludeWithout'];
201233

202234
/**
203235
* The size related validation rules.

tests/Validation/ValidationValidatorTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5204,6 +5204,21 @@ public function testExcludeUnless()
52045204
$this->assertSame(['mouse' => ['validation.required']], $validator->messages()->toArray());
52055205
}
52065206

5207+
public function testExcludeWithout()
5208+
{
5209+
$validator = new Validator(
5210+
$this->getIlluminateArrayTranslator(),
5211+
['region' => 'South'],
5212+
[
5213+
'country' => 'exclude_without:region|nullable|required_with:region|string|min:3',
5214+
'region' => 'exclude_without:country|nullable|required_with:country|string|min:3',
5215+
]
5216+
);
5217+
5218+
$this->assertTrue($validator->fails());
5219+
$this->assertSame(['country' => ['validation.required_with']], $validator->messages()->toArray());
5220+
}
5221+
52075222
public function testExcludeValuesAreReallyRemoved()
52085223
{
52095224
$validator = new Validator(

0 commit comments

Comments
 (0)