Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/Factory/FieldFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
use EasyCorp\Bundle\EasyAdminBundle\Field\BooleanField;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Field\CollectionField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateField;
use EasyCorp\Bundle\EasyAdminBundle\Field\DateTimeField;
Expand Down Expand Up @@ -187,7 +188,13 @@ private function replaceGenericFieldsWithSpecificFields(FieldCollection $fields,
}
/** @phpstan-ignore-next-line function.alreadyNarrowedType */
$fieldType = property_exists($fieldMapping, 'type') ? $fieldMapping->type : $fieldMapping['type'];
$guessedFieldFqcn = self::$doctrineTypeToFieldFqcn[$fieldType] ?? null;

// Special handling for enums, that are represented as string or as a simple array of strings
if ((Types::STRING === $fieldType || Types::SIMPLE_ARRAY === $fieldType) && isset($fieldMapping['enumType'])) {
$guessedFieldFqcn = ChoiceField::class;
} else {
$guessedFieldFqcn = self::$doctrineTypeToFieldFqcn[$fieldType] ?? null;
}
if (null === $guessedFieldFqcn) {
throw new \RuntimeException(sprintf('The Doctrine type of the "%s" field is "%s", which is not supported by EasyAdmin. For Doctrine\'s Custom Mapping Types have a look at EasyAdmin\'s field docs.', $fieldDto->getProperty(), $fieldType));
}
Expand Down
20 changes: 13 additions & 7 deletions src/Field/Configurator/ChoiceConfigurator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,18 +60,24 @@ public function configure(FieldDto $field, EntityDto $entityDto, AdminContext $c
}

if ($allChoicesAreEnums && array_is_list($choices) && \count($choices) > 0) {
$processedEnumChoices = [];
foreach ($choices as $choice) {
$processedEnumChoices[$choice->name] = $choice;
}

$choices = $processedEnumChoices;

// Update form type to be EnumType if current form type is still ChoiceType
// Leave the form type as is if user set something else explicitly
if (ChoiceType::class === $field->getFormType()) {
$field->setFormType(EnumType::class);
}

// When dealing with enums that implement TranslatableInterface, they are now translated by Symfony only if
// the keys of the choices are integers.
Comment on lines +69 to +70
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This sounds like a bug in Symfony?

Copy link
Author

@FoxCie FoxCie Nov 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They acknowledged that the previous behaviour was actually the bug. When dealing with choices, the keys of the array should be the labels. It is not explicit on EnumType, but on its parent ChoiceType, the doc states that clearly :
image

Source : https://symfony.com/doc/current/reference/forms/types/choice.html#choices .

The thing is that it was actually impossible to change an enum's labels with the choices array with the previous behaviour. Now it is possible, but the old behaviour is still present if the keys are integers (e.g. when giving the ::cases() as the choices).

EDIT : the associated bug in Symfony is here : symfony/symfony#61927 .

Copy link
Contributor

@Seb33300 Seb33300 Nov 30, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still looks wrong to me. The EnumType should transform the data to make it compatible with ChoiceType

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is what is done. It is just the label that can be changed by passing a string value as the key of the array. EnumType does what should be done so that the values are properly handled and converted. If you think this is not appropriate, you could open an issue on the Symfony side.

// So, keep choices with integer keys if using EnumType with translatable enum, otherwise set name as key.
if (!$choicesSupportTranslatableInterface || EnumType::class !== $field->getFormType()) {
$processedEnumChoices = [];
foreach ($choices as $choice) {
$processedEnumChoices[$choice->name] = $choice;
}

$choices = $processedEnumChoices;
}

$field->setFormTypeOptionIfNotSet('class', $enumTypeClass);
}

Expand Down
45 changes: 45 additions & 0 deletions tests/Controller/FormEnumTranslationControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\Controller;

use Doctrine\ORM\EntityRepository;
use EasyCorp\Bundle\EasyAdminBundle\Test\AbstractCrudTestCase;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\DashboardController;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller\FormEnumTranslationController;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\BlogPost;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Enum\BlogPostStateEnum;
use Symfony\Contracts\Translation\TranslatorInterface;

class FormEnumTranslationControllerTest extends AbstractCrudTestCase
{
protected EntityRepository $blogPosts;

protected function setUp(): void
{
parent::setUp();
$this->client->followRedirects();

$this->blogPosts = $this->entityManager->getRepository(BlogPost::class);
}

protected function getControllerFqcn(): string
{
return FormEnumTranslationController::class;
}

protected function getDashboardFqcn(): string
{
return DashboardController::class;
}

public function testFieldsFormatValue()
{
$translator = static::getContainer()->get(TranslatorInterface::class);
static::assertInstanceOf(TranslatorInterface::class, $translator);
$this->client->request('GET', $this->generateNewFormUrl());

foreach (BlogPostStateEnum::cases() as $case) {
self::assertAnySelectorTextSame('option', $case->trans($translator, locale: 'en'));
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Controller;

use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
use EasyCorp\Bundle\EasyAdminBundle\Field\ChoiceField;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\BlogPost;

/**
* @extends AbstractCrudController<BlogPost>
*/
class FormEnumTranslationController extends AbstractCrudController
{
public static function getEntityFqcn(): string
{
return BlogPost::class;
}

public function configureFields(string $pageName): iterable
{
// Test translating enums in forms. When enums implement TranslatableInterface, this should be done by Symfony
// automagically.
return [
ChoiceField::new('state', 'State'),
];
}
}
4 changes: 3 additions & 1 deletion tests/TestApplication/src/DataFixtures/AppFixtures.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\Page;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\User;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Entity\Website;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Enum\BlogPostStateEnum;

class AppFixtures extends Fixture
{
Expand Down Expand Up @@ -42,7 +43,8 @@ public function load(ObjectManager $manager): void
->setCreatedAt(new \DateTimeImmutable('2020-11-'.($i + 1).' 09:00:00'))
->setPublishedAt(new \DateTimeImmutable('2020-11-'.($i + 1).' 11:00:00'))
->addCategory($this->getReference('category'.($i % 10), Category::class))
->setAuthor($this->getReference('user'.($i % 5), User::class));
->setAuthor($this->getReference('user'.($i % 5), User::class))
->setState(BlogPostStateEnum::cases()[$i % \count(BlogPostStateEnum::cases())]);

if ($i < 10) {
$blogPost->setPublisher(
Expand Down
23 changes: 22 additions & 1 deletion tests/TestApplication/src/Entity/BlogPost.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Enum\BlogPostStateEnum;

#[ORM\Entity]
class BlogPost
Expand Down Expand Up @@ -39,6 +40,9 @@ class BlogPost
#[ORM\ManyToOne(targetEntity: User::class)]
private $publisher;

#[ORM\Column(enumType: BlogPostStateEnum::class)]
private ?BlogPostStateEnum $state = BlogPostStateEnum::Draft;

public function __construct()
{
$this->categories = new ArrayCollection();
Expand Down Expand Up @@ -150,8 +154,25 @@ public function getPublisher()
return $this->publisher;
}

public function setPublisher(?User $publisher): void
public function setPublisher(?User $publisher): self
{
$this->publisher = $publisher;

return $this;
}

public function getState(): ?BlogPostStateEnum
{
return $this->state;
}

public function setState(string|BlogPostStateEnum|null $state): self
{
if (!$state instanceof BlogPostStateEnum) {
$state = BlogPostStateEnum::tryFrom($state);
}
$this->state = $state;

return $this;
}
}
22 changes: 22 additions & 0 deletions tests/TestApplication/src/Enum/BlogPostStateEnum.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Tests\TestApplication\Enum;

use Symfony\Contracts\Translation\TranslatableInterface;
use Symfony\Contracts\Translation\TranslatorInterface;

enum BlogPostStateEnum: string implements TranslatableInterface
{
case Draft = 'draft';
case Published = 'published';
case Deleted = 'deleted';

public function trans(TranslatorInterface $translator, ?string $locale = null): string
{
return match ($this) {
self::Draft => $translator->trans('BlogPostStateEnum.draft', locale: $locale),
self::Published => $translator->trans('BlogPostStateEnum.published', locale: $locale),
self::Deleted => $translator->trans('BlogPostStateEnum.deleted', locale: $locale),
};
}
}
Loading