如何在单个选择(选择框)中变换Symfony中多个文本框的集合


How to transform Symfony collection of multiple text box in single choice (select box)

我有一个Task实体,假设每个Task属于多个Tag对象。

所以在形式上symfony文档我想要单个下拉而不是我有任务实体和标签实体关系一对多类似于http://symfony.com/doc/current/cookbook/form/form_collections.html

现在看到我的任务类型表单

Add ('tags', 'collection', ['type' => new choiceisttype (),'data_class' => 'AppBundle'Entity'Tag']}?>

//现在看到我的choiceisttype。php

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->addModelTransformer(new CollectionToChoice());
}
public function getParent()
{
    return 'choice';
}

在我的CollectionToChoice Transformer文件中的公共转换方法错误不能将"object"类型的值转换为有效的数组键。

更改表单,使标签不是一个集合,而是一个"entity"字段类型

然后您可以使用字段类型选项,选择是否显示复选框、单选或(多选)下拉

Element Type                          Expanded  Multiple
select tag                            false     false
select tag (with multiple attribute)  false     true
radio buttons                         true      false
checkboxes                            true      true

在你的例子中你可以将expand设为false, multiple设为true