From d710a0195f3d4cd5cb80290a94475ea0299a031f Mon Sep 17 00:00:00 2001 From: Daniel Bakan Date: Wed, 17 Jan 2024 22:16:48 +0100 Subject: [PATCH] fix Rule::unless for callable (#49726) --- src/Illuminate/Validation/Rule.php | 2 +- tests/Validation/ValidationRuleParserTest.php | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/src/Illuminate/Validation/Rule.php b/src/Illuminate/Validation/Rule.php index 1f8286cc375f..df28c8938c60 100644 --- a/src/Illuminate/Validation/Rule.php +++ b/src/Illuminate/Validation/Rule.php @@ -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); } /** diff --git a/tests/Validation/ValidationRuleParserTest.php b/tests/Validation/ValidationRuleParserTest.php index 2ead8eecbb9d..367d8b59c24f 100644 --- a/tests/Validation/ValidationRuleParserTest.php +++ b/tests/Validation/ValidationRuleParserTest.php @@ -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([ @@ -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); }