-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[Validator] Add support for enum in Choice
constraint
#59633
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -86,7 +86,7 @@ protected function formatValue(mixed $value, int $format = 0): string | |
} | ||
|
||
if ($value instanceof \UnitEnum) { | ||
return $value->name; | ||
$value = $value instanceof \BackedEnum ? $value->value : $value->name; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I personally don't think this is a good idea. Enum case names are helpful for developers, scalar equivalent less so. Moreover, this will have an impact on other constraints, right ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To be honest, I don't need this enum functionality in choice constraint (just had an idea & started coding), but this line of code should be fixed upstream. In the Btw this should not break anything, may third-party tests depending on this functionality. Haven't seen much enum usages in constraints and tests worked smooth for the other part of code. PS: Is there any reason why there's no |
||
} | ||
|
||
if (\is_object($value)) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\Validator\Tests\Fixtures; | ||
|
||
/** | ||
* @author Ninos Ego <me@ninosego.de> | ||
*/ | ||
enum TestEnumBackendInteger: int | ||
{ | ||
case FirstCase = 3; | ||
case SecondCase = 4; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Enums are already supported
This PR add supports for using a class-string to specify the list of possible values ( integer or string )