如何删除呈现的多余字段组?
How to remove superfluous field-groups rendered?
我正在使用 CollectionType
在我的表单中呈现 class 的多个实例。
但是在呈现输入字段后,呈现了多余的信息。
就像那样:
(因为我需要至少 10 个声誉才能 post 图片,我只能 post IMGUR-link)
https://i.imgur.com/WXPnCxo.png
控制器:
$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>
如何删除输入字段下方的多余内容?我对这个有点迷茫。(我想它与提供的数据的数组索引有关?)
亲切的问候
尝试将 {{ form_end(form) }}
替换为 {{ form_end(form, {'render_rest': false}) }}
感谢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()
用标签渲染它们(无论如何)。
我正在使用 CollectionType
在我的表单中呈现 class 的多个实例。
但是在呈现输入字段后,呈现了多余的信息。
就像那样: (因为我需要至少 10 个声誉才能 post 图片,我只能 post IMGUR-link)
https://i.imgur.com/WXPnCxo.png
控制器:
$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>
如何删除输入字段下方的多余内容?我对这个有点迷茫。(我想它与提供的数据的数组索引有关?)
亲切的问候
尝试将 {{ form_end(form) }}
替换为 {{ form_end(form, {'render_rest': false}) }}
感谢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()
用标签渲染它们(无论如何)。