diff --git a/src/Illuminate/Validation/Rules/Enum.php b/src/Illuminate/Validation/Rules/Enum.php index 9cd5f33363fc..a519e58b83ea 100644 --- a/src/Illuminate/Validation/Rules/Enum.php +++ b/src/Illuminate/Validation/Rules/Enum.php @@ -34,14 +34,14 @@ public function __construct($type) */ public function passes($attribute, $value) { - if (is_null($value) || ! function_exists('enum_exists') || ! enum_exists($this->type) || ! method_exists($this->type, 'tryFrom')) { - return false; - } - if ($value instanceof $this->type) { return true; } + if (is_null($value) || ! function_exists('enum_exists') || ! enum_exists($this->type) || ! method_exists($this->type, 'tryFrom')) { + return false; + } + try { return ! is_null($this->type::tryFrom($value)); } catch (TypeError $e) { diff --git a/tests/Validation/ValidationEnumRuleTest.php b/tests/Validation/ValidationEnumRuleTest.php index b0e53e81f7f6..b5fee7ba8c1e 100644 --- a/tests/Validation/ValidationEnumRuleTest.php +++ b/tests/Validation/ValidationEnumRuleTest.php @@ -47,6 +47,21 @@ public function testvalidationPassesWhenPassingInstanceOfEnum() $this->assertFalse($v->fails()); } + public function testvalidationPassesWhenPassingInstanceOfPureEnum() + { + $v = new Validator( + resolve('translator'), + [ + 'status' => PureEnum::one, + ], + [ + 'status' => new Enum(PureEnum::class), + ] + ); + + $this->assertFalse($v->fails()); + } + public function testValidationFailsWhenProvidingNoExistingCases() { $v = new Validator(