Skip to content

ChoiceType multiple = true, expanded = false throws a TransformationFailedException [2.8] #24107

@Mopster

Description

@Mopster
Q A
Bug report? yes
Feature request? no
BC Break report? no
RFC? no
Symfony version 2.8.27

Related : #21267

I have a custom Type (LocationType), extending ChoiceType which loads a (translated) list of locations from the Database. Which worked fine so far. Now I want to update this type to support multiple selections.

'multiple' => true,
'expanded' => false,

The configureOptions of the LocationType :

public function configureOptions(OptionsResolver $resolver)
    {
        parent::configureOptions($resolver);

        if ($this->requestStack) {
            if ($request = $this->requestStack->getCurrentRequest()) {
                $this->locale = RequestUtils::getLocale($request);
            }
        }

        $choiceList = $this->getChoiceList();
        $choices = $choiceList->getChoices();

        $resolver->setDefaults([
            'choices' => $choices,
            'disabled' => empty($choices),
            'choices_as_values' => true,
            'choice_label' => function ($value, $key, $index) use ($choiceList) {
                return $choiceList->getLabel($key);
            },
        ]);
    }

$choices contains :

'Tokyo' => 'tokyo',
'Osaka' => 'osaka',
'Nagoya' => 'nagoya',

When submitting the form with values, I get a validation error and after some debugging, I found the submitted data is stuctured as a nested array.

screenshot from 2017-09-06 17-05-03

Which throws an exception in ChoiceType :

        // To avoid issues when the submitted choices are arrays (i.e. array to string conversions),
        // we have to ensure that all elements of the submitted choice data are NULL, strings or ints.
        $builder->addEventListener(FormEvents::PRE_SUBMIT, function (FormEvent $event) {
            $data = $event->getData();

            if (!is_array($data)) {
                return;
            }

            foreach ($data as $v) {
                if (null !== $v && !is_string($v) && !is_int($v)) {
                    throw new TransformationFailedException('All choices submitted must be NULL, strings or ints.');
                }
            }
        }, 256);

This only happens when using the select tag (with multiple attribute). When setting 'expanded' = true and using the checkboxes, there is no problem.

When checking the HTML code, the name attribute contains 2 square brackets :

name="JobType[location][][]" 

Using dom editor changing this to

name="JobType[location][]"

allows the form to be submitted successfully and the values saved.

I've been trying to figure this one out for a few hours now, searching the web for answers, and I hope I'm correct here.

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