Mario Legenda
Mario Legenda

Reputation: 759

jQuery Chosen plugin not sending multiple data to Symfony2 app

I have a Symfony2 form that has a multiple select box. This is the code...

$builder
    ->add('resources', 'entity', array(
            'class' => 'AppBundle:Entity',
            'property' => 'name',
            'label' => 'product.titles.select_box',
            'attr' => array(
                'class' => 'chosen',
                'data-placeholder' => '-- Choose something --',
                'multiple' => true

            )
        ))

I initialize Chosen just like it should...

    $('.chosen').chosen({
        no_results_text: "Nothing found"
    });

But, when the data is submitted, i only receive the last selected entity, not everything. What could be the problem here?

If this is Chosen default behaviour, does anyone have any suggestions on some jquery plugin that send all the selected data?

Upvotes: 1

Views: 790

Answers (2)

Mario Legenda
Mario Legenda

Reputation: 759

Found the solution. I was stupid. This is the code i I was using.

$builder
   ->add('resources', 'entity', array(
        'class' => 'AppBundle:Entity',
        'property' => 'name',
        'label' => 'product.titles.select_box',
        'attr' => array(
            'class' => 'chosen',
            'data-placeholder' => '-- Choose something --',
            'multiple' => true
        )
    ))

This is the code that works. The only change is that multiple => true is defined in the root options array, not attr array. Stupid mistake.

$builder
   ->add('resources', 'entity', array(
        'class' => 'AppBundle:Entity',
        'property' => 'name',
        'label' => 'product.titles.select_box',
        'attr' => array(
            'class' => 'chosen',
            'data-placeholder' => '-- Choose something --'
        )
        'multiple' => true
    ))

Upvotes: 0

Artamiel
Artamiel

Reputation: 3762

Can you show us how you try to access your data after your form is submitted? I tried to reproduce your problem, but it seems it works just as fine. Sample form, rendering 2 multiple choice fields - one comming from entity, and one with provided array of values:

$builder = $this->createFormBuilder()
        ->add('color', 'entity',
            array(
                'class' => 'AppBundle:Color',
                'property' => 'name',
                'multiple' => true
            )
        )
        ->add('choiceColor', 'choice',
            array(
                'choices' => array(
                    'red' => 'red',
                    'blue' => 'blue',
                    'green' => 'green',
                    'yellow' => 'yellow'
                ),
                'multiple' => true
            )
        )
        ->add('submit', 'submit')
        ->getForm()
    ;

Then, simply dumping the form data after submit

$builder->handleRequest($request);

$data = $builder->getData();
var_dump($data);

I get the following result:

array (size=2)
  'color' => 
    object(Doctrine\Common\Collections\ArrayCollection)[447]
      private 'elements' => 
        array (size=2)
          0 => 
            object(AppBundle\Entity\Color)[466]
              ...
          1 => 
            object(AppBundle\Entity\Color)[467]
              ...
  'choiceColor' => 
    array (size=2)
      0 => string 'green' (length=5)
      1 => string 'yellow' (length=6)

As you can see, the field mapped to an entity returns ArrayCollection of objects, and the one mapped with plain array returns array (size=2)

So may be the problem is somewhere else.

Upvotes: 2

Related Questions