使用CollectionType时显示表单时遇到问题。它不显示AchatType输入,只标记“Achat”。
什么错了?我不明白..
我的AchatType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('civilite', ChoiceType::class, [
'choices' => [
'Mr' => 'Mr',
'Mme' => 'Mme'
]])
->add('nom', TextType::class)
->add('prenom', TextType::class)
->add('commande', EntityType::class, array(
'class' => 'AppBundle\Entity\Commande',
))
;
}
我的CommandeType:
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('nb_ticket', ChoiceType::class, [
'choices' => [
1 => '1',
2 => '2',
3 => '3',
4 => '4',
5 => '5',
6 => '6',
],
'label' => false,
'expanded' => true,
'multiple' => false,
])
->add('event', EntityType::class, array(
'class' => 'AppBundle\Entity\Event',
))
->add('email', EmailType::class)
->add('achats', CollectionType::class, array(
'entry_type' => AchatType::class,
))
// ->add('achats', AchatType::class)
;
}
在我的Twig模板中,我有:
<div class="text-center">
{{ form_start(form) }}
{% for index, nb_ticket in form.nb_ticket.children %}
{{ form_widget(nb_ticket)}}
{{ form_label(nb_ticket) }}
{% endfor %}
</div>
<div style="display:none">
{{ form_widget(form.event) }}
</div>
<!-- ======== ETAPE 2 ======== -->
{% if form.achats.civilite is defined %}
{{ form_widget(form.achats.civilite) }}
{{ form_widget(form.achats.prenom, {'attr':{ "placeholder":"Prenom" }}) }}
{{ form_widget(form.achats.nom, {'attr':{ "placeholder":"Nom" }}) }}
<div style="display:none">
{{ form_widget(form.achats.commande) }}
</div>
{% endif %}
<div class="text-white text-center">
{{ form_widget(form.email, {'attr':{ "placeholder":"Email" }})}}
</div>
{{ form_end(form) }}
结果与我一样:
{{ form_start(form) }}
{{ form_end(form) }}
我注意到form.achats.civilite没有定义..
如何让它显示所有输入字段或在form.achats中定义form.achats.civilite和其他字段?
答案 0 :(得分:2)
如果你有一个活跃的&#34; allow_add&#34;选项,可以通过&#39;原型来呈现此输入。选项:
->add('achats', CollectionType::class, array(
'entry_type' => AchatType::class,
'allow_add' => true,
'prototype' => true,
))
然后以表格形式:
{{ form_row(form.achats.vars.prototype}) }}
它应该有用。
有关更多信息,请参阅原型选项
的Symfony文档