我已发布我的解决方案作为答案。Symfony2多个同一类的实体在一个表单中(已解决)
我想渲染一个具有多个相同Class实体的表单。 我将显示2个字段,价格(type = text)和启用(type =复选框)。
我不知道这些实体有多少个,因此表单必须动态获取它们。
我曾尝试做以下
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('price', 'text', array(
'label' => 'Price',
'required' => true
))
->add('enabled','checkbox',array(
'label' => 'Use this currency',
))
;
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Osiris\Entity\Pricing',
'csrf_protection' => false
));
}
public function getName()
{
return 'pricingtype';
}
在我控制我创建了我的形式是这样的:
$pricingForm = $this->createFormBuilder($prices)
->add('items','collection',array(
'required' => false,
'prototype' => true,
'type' => new PricingType(),
))
->getForm()
;
在我的树枝我做的:
{% for price in form_pricing %}
<h2>Price</h2>
<div class="row">{{ form_widget(price) }}</div>
{% endfor %}
然而,它只带有h2价格和空行div与class = row。我觉得我在那里一半,但我不知道如何继续前进。 如果有人知道如何提交字段,我会很感激。
你有你想要的解决方案吗? – 2015-04-01 13:58:32
是的,我把它作为对我的问题的答案发布在下面。 – 2015-04-02 10:56:46