Skip to content

Commit

Permalink
fix Rule::unless for callable (#49726)
Browse files Browse the repository at this point in the history
  • Loading branch information
dbakan authored Jan 17, 2024
1 parent 72e9965 commit d710a01
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Illuminate/Validation/Rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public static function when($condition, $rules, $defaultRules = [])
*/
public static function unless($condition, $rules, $defaultRules = [])
{
return new ConditionalRules(! $condition, $rules, $defaultRules);
return new ConditionalRules($condition, $defaultRules, $rules);
}

/**
Expand Down
8 changes: 8 additions & 0 deletions tests/Validation/ValidationRuleParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ public function testConditionalRulesAreProperlyExpandedAndFiltered()
'zip' => ['required', Rule::when($isAdmin, function (Fluent $input) {
return ['min:2'];
})],
'when_cb_true' => Rule::when(fn () => true, ['required'], ['nullable']),
'when_cb_false' => Rule::when(fn () => false, ['required'], ['nullable']),
'unless_cb_true' => Rule::unless(fn () => true, ['required'], ['nullable']),
'unless_cb_false' => Rule::unless(fn () => false, ['required'], ['nullable']),
]);

$this->assertEquals([
Expand All @@ -39,6 +43,10 @@ public function testConditionalRulesAreProperlyExpandedAndFiltered()
'city' => ['required', 'min:2'],
'state' => ['required', 'min:2'],
'zip' => ['required', 'min:2'],
'when_cb_true' => ['required'],
'when_cb_false' => ['nullable'],
'unless_cb_true' => ['nullable'],
'unless_cb_false' => ['required'],
], $rules);
}

Expand Down

0 comments on commit d710a01

Please sign in to comment.