-
-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Add support for Enums in Choice fields #5740
Conversation
Update FieldFactory.php Update ChoiceConfigurator.php Update ChoiceConfigurator.php Update ChoiceConfigurator.php Update ChoiceConfigurator.php Update ChoiceConfigurator.php Update Actions.php Update Actions.php Update ChoiceConfigurator.php
I've fixed a couple of tests, but there's a test that still fails. To be honest, I don't even understand what I'm doing. I don't work much with Enums, so this is still confusing to me. Hopefully @abozhinov or @ksn135 can take a look here and suggest some fixes. Thanks. |
Hi @javiereguiluz ! yield Fields\ChoiceField::new('priority', 'admin_label.doc.field.priority')->setRequired(true)
->setFormTypeOption('choice_label', fn ($choice, $key, $value) => 'admin_label.enum.docPriority.'. $value); Current version of VERY ugly BUT working code: yield Fields\ChoiceField::new('priority', 'admin_label.doc.field.priority')->setRequired(true)
->setFormTypeOption('placeholder', false)
->setFormType(MyEnumType::class) // use transformer to convert enum to string and back
->setFormTypeOption('class', DocPriority::class) // needed for MyEnumType
->setChoices( // to correctly show translated labels
\in_array($pageName, [Crud::PAGE_INDEX, Crud::PAGE_DETAIL], true)
? DocPriority::choices() : DocPriority::cases()); P.S.: I don't really understand why that test crashes either, unfortunately. I'll try to figure it out a little later. |
Hi @javiereguiluz ! There is no FAILING test. Just run it locally. |
Hi @javiereguiluz and @ksn135,
|
Without changes in the ChoiceConfigurator has following error:
|
foreach ((array) $fieldValue as $selectedValue) { | ||
if (null !== $selectedLabel = $flippedChoices[$selectedValue] ?? null) { | ||
if ($selectedValue instanceof TranslatableInterface) { | ||
$choiceMessage = $selectedValue; | ||
} else { | ||
if (\is_object($selectedLabel) && enum_exists($selectedLabel::class)) { |
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.
Should also add && !$field->getFormTypeOption('choice_label')
Will anyone merge this changes to main branch? |
You can close this PR because I create new #5867 with all tests Passed. |
Closing in favor of #5944. Hopefully it fixes problems for good 🙏 Thanks! |
No description provided.