Symfony不是Render CollectionType

时间:2018-09-14 10:07:19

标签: php forms symfony twig symfony-forms

我为与控制器分开的数据收集类型创建了一个表单

它是我的控制器

/**
 * @Security("has_role('ROLE_USER')")
 * @Route("/phonebook/add", name="add")
 */
public function addPerson()
{
$person = new PhoneBookP();
$form = $this->createForm(PersoanlBookType::class, $person);

return $this->render(
    'default/add.html.twig',
    array('form' => $form->createView())
);
}

及其我的表单

->add('emails', CollectionType::class, array(
                'entry_type' => EmailType::class,
                'allow_add' => true,
                'prototype' => true,
                'allow_delete' => true,
                'attr'         => [
                    'class' => "emails-collection",
                ],
            ))

我的树枝是

{% block body %}
{{ form(form) }}
{% endblock %}

它没有错误,可以在任何公共字段中工作(例如NumberType,..),但在我的输出中不呈现CollectionType。我正在使用Symfony 4.我怎么了?

1 个答案:

答案 0 :(得分:0)

在呈现表单中,您需要执行以下操作:

$person = new PhoneBookP();
//addEmail() is the arraycollection in your PhoneBookP Entity
//to be able to load the form, you need to add one from your arraycollection
$person->addEmail(new Email());
$form = $this->createForm(PersoanlBookType::class, $person);