Symfony 2.8 - 表单集合类型仅呈现标签

时间:2016-11-03 09:16:45

标签: forms symfony formcollection symfony-2.8

在我的应用程序中,用户可以使用多种语言,因此UserLanguage个实体与ManyToOne关系相关。

在用户实体类中:

/**
 * @ORM\OneToMany(targetEntity="TraineeLanguage", mappedBy="trainee", cascade={"persist"}))
 */
private $languages;

public function __construct()
{
    $this->languages = new ArrayCollection();
}

在语言实体类中:

/**
 * @ORM\ManyToOne(targetEntity="Trainee", inversedBy="languages")
 * @ORM\JoinColumn(name="trainee_id", referencedColumnName="id")
 */
private $trainee;

现在我想用用户个人资料表单创建一个语言表单集合:

->add('languages', CollectionType::class, array(
    'type' => TraineeLanguageType::class,
    'required' => false
))

我的语言表格类:

class TraineeLanguageType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('language', TextType::class, array(
                'attr' => array(
                    'placeholder' => 'Language'
                ),
                'required' => false
            ))
            ->add('level', TextType::class, array(
                'attr' => array(
                    'placeholder' => 'Level'
                ),
                'required' => false
            ));
    }
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver
            ->setDefaults(array(
                'data_class' => 'IndexBundle\Entity\TraineeLanguage'
            ));
    }

    public function getName()
    {
        return 'language';
    }
}

现在,当我尝试显示表单时

{{ form_row(trainee_background_information_form.languages) }}

仅显示标签。

它也无法使用:

{{ form_widget(trainee_background_information_form.languages) }}

{% for language in trainee_background_information_form.languages %}
    {{ form_row(language) }}
{% endfor %}

有没有人注意到我的代码中的错误并知道发生了什么?

1 个答案:

答案 0 :(得分:2)

当您使用Collection Type时,您需要编写额外的html& amp; js,处理这种类型的字段。更多内容在Symfony文档中:http://symfony.com/doc/current/reference/forms/types/collection.html