在Twig和Symfony中多次渲染字段

时间:2016-05-23 00:02:22

标签: symfony twig symfony-forms

我需要两个输入,以便用户可以选择

控制器

$etud = new Etudiant();
$form=$this->createFormBuilder($etud)
    ->add('filierechoisit',EntityType::class,array('class'=>'inscriptionBundle\Entity\filieres', 'choice_label'=>'libelle_filiere'))
    ->add('filierechoisit',EntityType::class,array('class'=>'inscriptionBundle\Entity\filieres', 'choice_label'=>'libelle_filiere'))->getForm();
if ($form->isValid()) {
    // ... maybe do some form processing, like saving the Task and Tag objects
}

return $this->render('inscriptionBundle:Default:authentification.html.twig', array(
    'modif' => $form->createView(),
));

我该怎么做?

2 个答案:

答案 0 :(得分:1)

我几乎可以肯定您希望ChoiceType / EntityType字段的multipleexpanded选项为true。 它应该是这样的:

$form->add('filierechoisit', EntityType::class, array(
    # query choices from this entity
    'class' => 'inscriptionBundle\Entity\filieres',
    # use the filieres.libelle_filiere property as the visible option string
    'choice_label' => 'libelle_filiere',
    # used to render a select box, check boxes or radios
    'multiple' => true,
    'expanded' => true,
));

答案 1 :(得分:0)

您正在混合表单处理和表单呈现。如果您希望用户选择他输入数据的方式 - 您不希望以两种不同的方式处理此数据,直到这两个独立的字段为止。

你应该只有一个

->add('filierechoisit',EntityType::class,array('class'=>'inscriptionBundle\Entity\filieres', 'choice_label'=>'libelle_filiere'))

调用添加字段处理并保留前端的所有渲染。您可以在那里使用一些JS或API,或者在简单的情况下,只需覆盖该字段的Twig模板

http://symfony.com/doc/current/cookbook/form/form_customization.html

您可以在此处为表单呈现自己的小部件,允许用户执行一些html内容来更改输入。

目前,使两个具有相同名称的add调用只会使第二个覆盖第一个。