Skip to content

[2.7] [Form] Regression on non required ChoiceType handling #14393

@hhamon

Description

@hhamon

As of 03efce1, the ChoiceType is broken when dealing with empty value (or required = false option). Take the following simple example to illustrate the problem:

<?php
// ...
class DefaultController extends Controller
{
    /** @Route("/bug") */
    public function bugAction(Request $request)
    {
        $form = $this
            ->createFormBuilder()
            ->add('assignee', 'choice', [
                'choices' => [
                    'james' => 'James',
                    'philip' => 'Philip',
                    'elisabeth' => 'Elisabeth',
                ],
                'required' => false,
            ])
            ->getForm()
        ;
        $form->handleRequest($request);

        if ($form->isValid()) {
            die('Yeah!');
        }

        return $this->render('default/bug.html.twig', [
            'form' => $form->createView(),
        ]);
    }
}

The created form has one child field called assignee. It's a dropdown list of persons and it automatically includes an empty value when it's rendered thanks to the required option set to false. But when I submit the form with the empty value selected, an error message is displayed in the template saying: This value is not valid.. The data transformer used to convert the submitted view data asks the ArrayKeyChoiceList for the value ''. But the latter doesn't have it so it throws a TransformationFailedException.

This regression affects all choice based form field types including date, datetime, birthday...

We still have less than 2 weeks before releasing Symfony 2.7.0, so this bug must be fixed asap.

@webmozart can you look at the fix or tell me where I should make the fix please?

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