Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static function can($ability, ...$arguments)
}

/**
* Create a new conditional rule set.
* Apply the given rules if the given condition is truthy.
*
* @param callable|bool $condition
* @param array|string|\Closure $rules
Expand All @@ -46,6 +46,19 @@ public static function when($condition, $rules, $defaultRules = [])
return new ConditionalRules($condition, $rules, $defaultRules);
}

/**
* Apply the given rules if the given condition is falsy.
*
* @param callable|bool $condition
* @param array|string|\Closure $rules
* @param array|string|\Closure $defaultRules
* @return \Illuminate\Validation\ConditionalRules
*/
public static function unless($condition, $rules, $defaultRules = [])
{
return new ConditionalRules(! $condition, $rules, $defaultRules);
}

/**
* Create a new nested rule set.
*
Expand Down
42 changes: 26 additions & 16 deletions tests/Validation/ValidationRuleParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,21 @@ class ValidationRuleParserTest extends TestCase
{
public function testConditionalRulesAreProperlyExpandedAndFiltered()
{
$isAdmin = true;

$rules = ValidationRuleParser::filterConditionalRules([
'name' => Rule::when(true, ['required', 'min:2']),
'email' => Rule::when(false, ['required', 'min:2']),
'password' => Rule::when(true, 'required|min:2'),
'username' => ['required', Rule::when(true, ['min:2'])],
'address' => ['required', Rule::when(false, ['min:2'])],
'name' => Rule::when($isAdmin, ['required', 'min:2']),
'email' => Rule::unless($isAdmin, ['required', 'min:2']),
'password' => Rule::when($isAdmin, 'required|min:2'),
'username' => ['required', Rule::when($isAdmin, ['min:2'])],
'address' => ['required', Rule::unless($isAdmin, ['min:2'])],
'city' => ['required', Rule::when(function (Fluent $input) {
return true;
}, ['min:2'])],
'state' => ['required', Rule::when(true, function (Fluent $input) {
'state' => ['required', Rule::when($isAdmin, function (Fluent $input) {
return 'min:2';
})],
'zip' => ['required', Rule::when(false, [], function (Fluent $input) {
'zip' => ['required', Rule::when($isAdmin, function (Fluent $input) {
return ['min:2'];
})],
]);
Expand All @@ -42,16 +44,20 @@ public function testConditionalRulesAreProperlyExpandedAndFiltered()

public function testEmptyRulesArePreserved()
{
$isAdmin = true;

$rules = ValidationRuleParser::filterConditionalRules([
'name' => [],
'email' => '',
'password' => Rule::when(true, 'required|min:2'),
'password' => Rule::when($isAdmin, 'required|min:2'),
'gender' => Rule::unless($isAdmin, 'required'),
]);

$this->assertEquals([
'name' => [],
'email' => '',
'password' => ['required', 'min:2'],
'gender' => [],
], $rules);
}

Expand All @@ -64,12 +70,14 @@ public function testEmptyRulesCanBeExploded()

public function testConditionalRulesWithDefault()
{
$isAdmin = true;

$rules = ValidationRuleParser::filterConditionalRules([
'name' => Rule::when(true, ['required', 'min:2'], ['string', 'max:10']),
'email' => Rule::when(false, ['required', 'min:2'], ['string', 'max:10']),
'password' => Rule::when(false, 'required|min:2', 'string|max:10'),
'username' => ['required', Rule::when(true, ['min:2'], ['string', 'max:10'])],
'address' => ['required', Rule::when(false, ['min:2'], ['string', 'max:10'])],
'name' => Rule::when($isAdmin, ['required', 'min:2'], ['string', 'max:10']),
'email' => Rule::unless($isAdmin, ['required', 'min:2'], ['string', 'max:10']),
'password' => Rule::unless($isAdmin, 'required|min:2', 'string|max:10'),
'username' => ['required', Rule::when($isAdmin, ['min:2'], ['string', 'max:10'])],
'address' => ['required', Rule::unless($isAdmin, ['min:2'], ['string', 'max:10'])],
]);

$this->assertEquals([
Expand All @@ -83,10 +91,12 @@ public function testConditionalRulesWithDefault()

public function testEmptyConditionalRulesArePreserved()
{
$isAdmin = true;

$rules = ValidationRuleParser::filterConditionalRules([
'name' => Rule::when(true, '', ['string', 'max:10']),
'email' => Rule::when(false, ['required', 'min:2'], []),
'password' => Rule::when(false, 'required|min:2', 'string|max:10'),
'name' => Rule::when($isAdmin, '', ['string', 'max:10']),
'email' => Rule::unless($isAdmin, ['required', 'min:2']),
'password' => Rule::unless($isAdmin, 'required|min:2', 'string|max:10'),
]);

$this->assertEquals([
Expand Down