0

I have 2 entities ItemSelector which can have several Item. In the view i then want to display a itemselector, with select box of Item. I've read the doc for the prototype part, to have a dynamic add/remove of Item.

The problem is the values passed to the form for item are not selected in the select.

I must be missing a parameter somewhere but i can't find it. I know the values are passed to the form because i can display them (outside the select box)

enter image description here

The item is not selected: below ecn-exo1

enter image description here My view :

        <ul class="isel-item" data-prototype="{{ _self.isel_prototype(form.items.vars.prototype)|e }}">
            {% for item in form.items %}
                <li class="item">
                    <table class="table">
                        <tr>
                            <td class="isel_label">{{ form_label(item.itemcode) }} - {{ item.vars.value.itemcode }}</td>
                            <td class="isel_widget">{{ form_widget(item.itemcode) }}{{ form_errors(item.itemcode) }}</td>
                        </tr>
                    </table>
                </li>
            {% endfor %}
        </ul>

My Controller

    public function chooseAction(Request $request, ItemSelector $itemSelector)
    {
        $em = $this->getDoctrine()->getManager();

        // Create an ArrayCollection of the current Item objects in the database
        $originalItems = new ArrayCollection();
        foreach ($itemSelector->getItems() as $item) {
            $originalItems->add($item);
        }

        /*
         * Begin dummy init
         */
        $item1 = new Item();
        $item1->setItemcode(36);
        $item2 = new Item();
        $item2->setItemcode(38);
        $itemSelector->addItem($item1);
        $itemSelector->addItem($item2);
        /*
         * End dummy init
         */

        $form = $this->get('form.factory')
            ->create(new ItemSelectorType(), $itemSelector);

        $form->handleRequest($request);

        if ($form->isValid()) {

            // remove the relationship between the item and the ItemSelector
            foreach ($originalItems as $item) {

                if (false === $itemSelector->getItems()->contains($item)) {

                    // in a a many-to-one relationship, remove the relationship
                    $item->setItemSelector(null);

                    $em->persist($item);

                    // to delete the Item entirely, you can also do that
                    $em->remove($item);
                }
            }
            $em->persist($itemSelector);
            $em->flush();
        }

        return array(
            '_resource' => $itemSelector,
            'form'      => $form->createView(),
        );
    }
}

The form type : ItemSelectorType

public function buildForm(FormBuilderInterface $builder, array $options)
{
        ...

        $builder
            ->add(
                'items', 'collection', array(
                    'type'          => new ItemType(),
                    'by_reference'  => false,
                    'prototype'     => true,
                    'allow_add'     => true,
                    'allow_delete'  => true,
                )
            )
        ;
}

And ItemType :

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add(
            'itemcode', 'entity', array(
                'label'         => 'Code',
                'class'         => 'NWAItemSelectorBundle:ItemSelectorResourceNode,
                'choice_label'  =>'name',
                'empty_value'   => 'Choose an item',
                'query_builder' => function(ItemSelectorResourceNodeRepository $er) use ($resourceType, $namePattern) {
                    return $er->getQbFilteredBy($resourceType, $namePattern);
                }
            )
        );
}

The ItemSelectorResourceNodeRepository::getQbFilteredBy allow to filter the data to be displayed.

Overdose
  • 585
  • 7
  • 30

2 Answers2

0

Please read this http://symfony.com/blog/new-in-symfony-2-7-choice-form-type-refactorization#dynamic-generation-of-choice-labels

Also, you can use either choice_value or choices_as_values => true

Seif Sayed
  • 783
  • 2
  • 9
  • 18
  • instead `->add('itemcode', 'entity'...)` you mean `->add('itemcode', 'choice'...)` ? or just the part concerning the label ? The problem is with the choice value, not the choice label since this is the "value" that is used for the selected part. Moreover, you think this is a problem in the FormType, and not in the view ? but then, how come i can display the correct value with `item.vars.value.items` ? – Overdose Dec 09 '15 at 17:58
  • My values in the select are correct : the value for the item is in the selectbox list, **but it is not selected.** – Overdose Dec 09 '15 at 18:05
0

I'm pretty new to Symfony however this looks like it might help.

Symfony2 Setting a default choice field selection

it looks like they are passing in the array:

'data' => value

when adding the field to the form.

public function buildForm(FormBuilderInterface $builder, array $options)
{
    ...

    $builder
        ->add(
            'items', 'collection', array(
                'type'          => new ItemType(),
                'by_reference'  => false,
                'prototype'     => true,
                'allow_add'     => true,
                'allow_delete'  => true,
                'data'  => 3,
            )
        )
    ;
}
Community
  • 1
  • 1
  • Thanks. I've seent hat post before. But, 1 - The values of items are passed (36, 38) as shown in the pictures attached. 2 - i tried anyways to pass the items in 'data', but it changed nothing. 3 - In the post you gave [another user showed] (http://stackoverflow.com/questions/8073236/symfony2-setting-a-default-choice-field-selection#answer-19720484) there should be no need for such explicit data input. – Overdose Dec 10 '15 at 07:29