我正在使用CollectionType
在表单中呈现一个类的多个实例。
但是在渲染输入字段之后,会渲染出多余的信息。
就像这样: (由于我需要至少10个信誉才能发布图像,所以我只能发布IMGUR链接)
控制器:
$leadPartnerList = $LeadPartnerLoader->loadAll();
$formBuilderData = [
'lead_partners' => $leadPartnerList
];
$form = $this->createFormBuilder($formBuilderData)
->add('lead_partners', CollectionType::class, [
'entry_type' => LeadPartnerFormType::class,
'entry_options' => [
'label' => null
]
])->getForm();
$form->handleRequest($request);
if($form->isSubmitted() && $form->isValid())
{
$data = $form->getData();
dump($data);
}
return $this->render(
'lead_partner_overview2.html.twig',
[
'form' => $form->createView()
]);
LeadPartnerFormType:
class LeadPartnerFormType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'data_class' => LeadPartner::class,
));
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('id', HiddenType::class)
->add('name', TextType::class);
}
}
Twig模板:
<div>
{{ form_start(form) }}
{% for partner in form.lead_partners %}
{{ form_row(partner.name) }}
{% endfor %}
{{ form_end(form) }}
</div>
如何删除输入字段下方的多余内容?我对此有些迷惑(我想它与所提供数据的数组索引有关吗?)
亲切问候
答案 0 :(得分:1)
尝试将path='*'
替换为{{ form_end(form) }}
答案 1 :(得分:1)
由于本杰明·科兹洛夫斯基(Benjamin Kozlowski)的回答,我抬起头来form_rest(...)
:
将模板更改为
{{ form_start(form) }}
{% for partner in form.lead_partners %}
{{ form_row(partner.id) }}
{{ form_row(partner.name) }}
{% endfor %}
{{ form_end(form) }}
解决了这个问题,因为我忘记了渲染隐藏的id字段,因此form_rest()
用标签(无论如何)来渲染它们。