Symfony2在选择中显示值

时间:2015-12-09 11:59:38

标签: forms symfony nested

我有2个实体 ItemSelector可以有多个Item。 在视图中,我想显示一个itemselector,其中包含Item的选择框。我已经阅读了原型部分的文档,以动态添加/删除Item。

问题是未在选择中选择传递给项目表单的值。

我必须在某个地方错过参数,但我找不到它。我知道值传递给表单,因为我可以显示它们(在选择框之外)

enter image description here

未选择该项目:在ecn-exo1下面

enter image description here 我的观点:

        <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>

我的控制器

    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(),
        );
    }
}

表单类型: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,
                )
            )
        ;
}

和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);
                }
            )
        );
}

ItemSelectorResourceNodeRepository :: getQbFilteredBy允许过滤要显示的数据。

2 个答案:

答案 0 :(得分:0)

请阅读此http://symfony.com/blog/new-in-symfony-2-7-choice-form-type-refactorization#dynamic-generation-of-choice-labels

此外,您可以使用choice_value或choices_as_values =&gt;真

答案 1 :(得分:0)

我对Symfony很新,但这看起来可能有所帮助。

Symfony2 Setting a default choice field selection

看起来他们正在传递数组:

'data' => value
将字段添加到表单时

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,
            )
        )
    ;
}