Pedy77
Pedy77

Reputation: 1

symfony2 formbuilder choice multiple throws no array

Issue: Multiple dropdown just hands over a string not an array.

I tried to use a multiple dropdown in the formbuilder:

->add('options', 'choice', array(
                    'choices' => $printerOptionsDropdown,
                    'empty_value' => 'Optionen wählen',
                    'label' => 'Optionen',
                    'attr' => array(
                        'class' => 'form-control selectpicker',
                        'data-live-search' => true,
                        'multiple' => true),
                    'required' => false
                ))

With this twig template:

<form action="{{ path('<form>_create', { 'id' : entity.id }) }}" name="<formForm>" id="<formForm>" method="POST" class="form-horizontal" role="form" >
        <div class="form-group">
            <label for="<formbuildertag>_options" class="col-sm-2 control-label">{{ form_label(form.options) }}</label>
            <div class="col-sm-4">
                {{ form_widget(form.options) }}{{ form_errors(form.options) }}
            </div>
        </div>

And everything looks fine. I can select multiple options. But when I submit the form it only hands over a string not an array.

<formbuildertag>[options]:"Value1"
<formbuildertag>[options]:"Value2"

The output of the post request is just a string of Value2. It gets overwritten because it's not an array. I got that. But why does the formbuilder not even create an array for the form.

I already tried to overwrite the full_name

form_widget(form.options, `enter code here`'full_name' => '<formbuldertag>[options][]')

but it didn't work.

Any ideas?

Upvotes: 0

Views: 51

Answers (1)

Jeet
Jeet

Reputation: 1587

You must have multiple option defined as true. You have it in attr. change it as below :

->add('options', 'choice', array(
    'choices' => $printerOptionsDropdown,
    'empty_value' => 'Optionen wählen',
    'label' => 'Optionen',
    'attr' => array(
        'class' => 'form-control selectpicker',
        'data-live-search' => true,
    'required' => false,
    'multiple' => true
))

Hope this helps!

Upvotes: 1

Related Questions