Skip to content

Commit 9eca262

Browse files
committed
Preserve falsey values in Can validation rule
1 parent 5c5c1f3 commit 9eca262

2 files changed

Lines changed: 20 additions & 1 deletion

File tree

src/Illuminate/Validation/Rules/Can.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ public function passes($attribute, $value)
5454

5555
$model = array_shift($arguments);
5656

57-
return Gate::allows($this->ability, array_filter([$model, ...$arguments, $value]));
57+
return Gate::allows($this->ability, array_filter(
58+
[$model, ...$arguments, $value], fn ($argument) => ! is_null($argument)
59+
));
5860
}
5961

6062
/**

tests/Validation/ValidationRuleCanTest.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,23 @@ public function testValidationPasses()
9292
$this->assertTrue($v->passes());
9393
}
9494

95+
public function testValidationPassesFalseyArguments()
96+
{
97+
$this->gate()->define('update-company', function ($user, ...$arguments) {
98+
$this->assertSame([\App\Models\Company::class, 0, false, '0', 0], $arguments);
99+
100+
return true;
101+
});
102+
103+
$v = new Validator(
104+
resolve('translator'),
105+
['company' => 0],
106+
['company' => new Can('update-company', [\App\Models\Company::class, 0, false, '0'])]
107+
);
108+
109+
$this->assertTrue($v->passes());
110+
}
111+
95112
public function testCustomMessageUsingDotNotationAndFqcnWorks()
96113
{
97114
$v = new Validator(

0 commit comments

Comments
 (0)