10000 bug #50017 [Validator] Fix support of Enum to `ConstraintValidator::f… · symfony/symfony@831e0ca · GitHub
[go: up one dir, main page]

Skip to content

Commit 831e0ca

Browse files
bug #50017 [Validator] Fix support of Enum to ConstraintValidator::formatValue (PhoneixS)
This PR was submitted for the 6.2 branch but it was squashed and merged into the 5.4 branch instead. Discussion ---------- [Validator] Fix support of Enum to `ConstraintValidator::formatValue` | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #50000 | License | MIT I have added a simple check in the `formatValue` to see if the object is an enum and in that case, return the name of the enum instead of the "object" string. Commits ------- 128b3da [Validator] Fix support of Enum to `ConstraintValidator::formatValue`
2 parents 1ec5dac + 128b3da commit 831e0ca

File tree

2 files changed

+11
-0
lines changed

2 files changed

+11
-0
lines changed

src/Symfony/Component/Validator/ConstraintValidator.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ protected function formatValue($value, int $format = 0)
9999
return $value->format('Y-m-d H:i:s');
100100
}
101101

102+
if ($value instanceof \UnitEnum) {
103+
return $value->name;
104+
}
105+
102106
if (\is_object($value)) {
103107
if (($format & self::OBJECT_TO_STRING) && method_exists($value, '__toString')) {
104108
return $value->__toString();

src/Symfony/Component/Validator/Tests/ConstraintValidatorTest.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public static function formatValueProvider()
4141
['array', []],
4242
['object', $toString = new TestToStringObject()],
4343
['ccc', $toString, ConstraintValidator::OBJECT_TO_STRING],
44+
['FirstCase', $toString = TestEnum::FirstCase],
4445
['object', $dateTime = new \DateTimeImmutable('1971-02-02T08:00:00UTC')],
4546
[class_exists(\IntlDateFormatter::class) ? 'Oct 4, 2019, 11:02 AM' : '2019-10-04 11:02:03', new \DateTimeImmutable('2019-10-04T11:02:03+09:00'), ConstraintValidator::PRETTY_DATE],
4647
[class_exists(\IntlDateFormatter::class) ? 'Feb 2, 1971, 8:00 AM' : '1971-02-02 08:00:00', $dateTime, ConstraintValidator::PRETTY_DATE],
@@ -73,3 +74,9 @@ public function __toString()
7374
return 'ccc';
7475
}
7576
}
77+
78+
enum TestEnum
79+
{
80+
case FirstCase;
81+
case SecondCase;
82+
}

0 commit comments

Comments
 (0)
0