Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\Options;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Contracts\Translation\TranslatableInterface;

/**
* A choice type for native PHP enums.
Expand All @@ -29,7 +30,7 @@ public function configureOptions(OptionsResolver $resolver): void
->setAllowedTypes('class', 'string')
->setAllowedValues('class', enum_exists(...))
->setDefault('choices', static fn (Options $options): array => $options['class']::cases())
->setDefault('choice_label', static fn (\UnitEnum $choice): string => $choice->name)
->setDefault('choice_label', static fn (\UnitEnum $choice) => $choice instanceof TranslatableInterface ? $choice : $choice->name)
->setDefault('choice_value', static function (Options $options): ?\Closure {
if (!is_a($options['class'], \BackedEnum::class, true)) {
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,11 @@
use Symfony\Component\Form\Tests\Fixtures\Answer;
use Symfony\Component\Form\Tests\Fixtures\Number;
use Symfony\Component\Form\Tests\Fixtures\Suit;
use Symfony\Component\Form\Tests\Fixtures\TranslatableTextAlign;
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException;
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException;
use Symfony\Component\Translation\IdentityTranslator;
use Symfony\Contracts\Translation\TranslatableInterface;

class EnumTypeTest extends BaseTypeTestCase
{
Expand Down Expand Up @@ -257,6 +260,20 @@ public function testChoiceLabel()
$this->assertSame('Yes', $view->children[0]->vars['label']);
}

public function testChoiceLabelTranslatable()
{
$form = $this->factory->create($this->getTestedType(), null, [
'multiple' => false,
'expanded' => true,
'class' => TranslatableTextAlign::class,
]);

$view = $form->createView();

$this->assertInstanceOf(TranslatableInterface::class, $view->children[0]->vars['label']);
$this->assertEquals('Left', $view->children[0]->vars['label']->trans(new IdentityTranslator()));
}

protected function getTestOptions(): array
{
return ['class' => Suit::class];
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Form\Tests\Fixtures;

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

enum TranslatableTextAlign implements TranslatableInterface
{
case Left;
case Center;
case Right;

public function trans(TranslatorInterface $translator, string $locale = null): string
{
return $translator->trans($this->name, locale: $locale);
}
}