Skip to content

[Form] EnumType contains null in choice_filter callback #47885

Closed
@pavol-tuka

Description

@pavol-tuka

Symfony version(s) affected

6.1.6

Description

If you use EnumType with choice_filter option and callback function, first value passed to callback is null. Option required is true by default, so null should not be there.

How to reproduce

src/Enum/TestEnum.php

<?php

declare(strict_types=1);

namespace App\Enum;

enum TestEnum: string
{
    case A = 'A';
    case B = 'B';
    case C = 'C';
}

src/Controller/TestController.php

<?php

declare(strict_types=1);

namespace App\Controller;

use App\Enum\TestEnum;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
use function dump;

class TestController extends \Symfony\Bundle\FrameworkBundle\Controller\AbstractController
{
    #[Route('/test')]
    public function __invoke(): Response
    {
        $builder = $this->createFormBuilder();

        $builder->add('test', EnumType::class, [
            'class' => TestEnum::class,
            'choice_filter' => function ($x) {
                dump($x);
                return true;
            },
        ]);

        return $this->render('test.html.twig', ['form' => $builder->getForm()->createView()]);
    }
}

templates/test.html.twig

<body>
    {{ form(form) }}
</body>

Possible Solution

No response

Additional Context

image

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions