【发布时间】:2015-03-31 14:52:16
【问题描述】:
我想呈现一个具有多个同类实体的表单。 我将显示 2 个字段,Price(type=text) 和 Enabled(type=checkbox)。
我不知道我会有多少这些实体,所以表单必须动态获取它们。
我尝试过以下操作
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 价格和 class=row 的空 div。我觉得我已经成功了一半,但我不知道如何继续前进。 如果有人也知道如何在提交时获取字段,我将不胜感激。
【问题讨论】:
-
您找到了您想要的解决方案?
-
是的,我将其发布为下面我的问题的答案。
标签: php forms symfony doctrine-orm