Skip to content

Commit b6b4a19

Browse files
authored
fix(enums): make Enum rule accept pure enums when passing enum instance (#45121)
1 parent 11edbfe commit b6b4a19

File tree

2 files changed

+19
-4
lines changed

2 files changed

+19
-4
lines changed

src/Illuminate/Validation/Rules/Enum.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,14 @@ public function __construct($type)
3434
*/
3535
public function passes($attribute, $value)
3636
{
37-
if (is_null($value) || ! function_exists('enum_exists') || ! enum_exists($this->type) || ! method_exists($this->type, 'tryFrom')) {
38-
return false;
39-
}
40-
4137
if ($value instanceof $this->type) {
4238
return true;
4339
}
4440

41+
if (is_null($value) || ! function_exists('enum_exists') || ! enum_exists($this->type) || ! method_exists($this->type, 'tryFrom')) {
42+
return false;
43+
}
44+
4545
try {
4646
return ! is_null($this->type::tryFrom($value));
4747
} catch (TypeError $e) {

tests/Validation/ValidationEnumRuleTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,21 @@ public function testvalidationPassesWhenPassingInstanceOfEnum()
5252
$this->assertFalse($v->fails());
5353
}
5454

55+
public function testvalidationPassesWhenPassingInstanceOfPureEnum()
56+
{
57+
$v = new Validator(
58+
resolve('translator'),
59+
[
60+
'status' => PureEnum::one,
61+
],
62+
[
63+
'status' => new Enum(PureEnum::class),
64+
]
65+
);
66+
67+
$this->assertFalse($v->fails());
68+
}
69+
5570
public function testValidationFailsWhenProvidingNoExistingCases()
5671
{
5772
$v = new Validator(

0 commit comments

Comments
 (0)