Mario0
Mario0

Reputation: 33

Symfony2 Multiple Select on Form Builder default values

This has been boggling my mind for hours now, I am using Symfony2's form builder!

->add('technicians', 'choice', array(
                'choices'   => array(
                    1 => 'Test'
                ),
                'multiple'  => true,
                'data' => array(
                    1 => true
                )
            ))

Image below:

https://i.sstatic.net/aUi7H.png

But when I use Strings as keys in the array, it magically stops working.

Like so:

->add('technicians', 'choice', array(
                'choices'   => array(
                    'example' => 'Example'
                ),
                'multiple'  => true,
                'data' => array(
                    'example' => true
                )
            ))

https://i.sstatic.net/nB1pi.png

Upvotes: 1

Views: 1690

Answers (1)

Ioana Hazsda
Ioana Hazsda

Reputation: 580

It works with:

 ->add('technicians', 'choice', array(
            'choices'   => array(
                  'example' => 'Example'
                ),
             'multiple'  => true,
             'data' => array(
                   'example' => 'example'
              ),
       ))

The value for data should match the key of the choices array.

true == 1, that's why your first example was working.

Upvotes: 6

Related Questions