Skip to content

Commit fbfe9ad

Browse files
authored
Handle null as a falsy condition (#56612)
1 parent 326165a commit fbfe9ad

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

src/Illuminate/Validation/Rules/RequiredIf.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,14 @@ class RequiredIf implements Stringable
1818
/**
1919
* Create a new required validation rule based on a condition.
2020
*
21-
* @param (\Closure(): bool)|bool $condition
21+
* @param (\Closure(): bool)|bool|null $condition
2222
*/
2323
public function __construct($condition)
2424
{
25+
if (is_null($condition)) {
26+
$condition = false;
27+
}
28+
2529
if ($condition instanceof Closure || is_bool($condition)) {
2630
$this->condition = $condition;
2731
} else {

tests/Validation/ValidationRequiredIfTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,5 +77,10 @@ public function testRequiredIfRuleValidation()
7777

7878
$v = new Validator($trans, ['x' => 'foo'], ['x' => ['string', $rule]]);
7979
$this->assertTrue($v->passes());
80+
81+
$rule = new RequiredIf(null);
82+
83+
$v = new Validator($trans, ['x' => 'foo'], ['x' => ['string', $rule]]);
84+
$this->assertTrue($v->passes());
8085
}
8186
}

0 commit comments

Comments
 (0)