Symfony 2.8 - 表单集合类型仅渲染标签
Symfony 2.8 - Form Collection Type rendering only label
在我的应用程序中,用户可以使用多种语言,因此 User
和 Language
实体通过 ManyToOne
关系关联。
在用户实体中 class:
/**
* @ORM\OneToMany(targetEntity="TraineeLanguage", mappedBy="trainee", cascade={"persist"}))
*/
private $languages;
public function __construct()
{
$this->languages = new ArrayCollection();
}
语言实体class:
/**
* @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:
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 %}
有没有人注意到我的代码中的错误并知道发生了什么?
当你使用Collection Type时,你需要额外编写html & js,来处理这种类型的字段。更多内容在 Symfony 文档中:http://symfony.com/doc/current/reference/forms/types/collection.html
在我的应用程序中,用户可以使用多种语言,因此 User
和 Language
实体通过 ManyToOne
关系关联。
在用户实体中 class:
/**
* @ORM\OneToMany(targetEntity="TraineeLanguage", mappedBy="trainee", cascade={"persist"}))
*/
private $languages;
public function __construct()
{
$this->languages = new ArrayCollection();
}
语言实体class:
/**
* @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:
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 %}
有没有人注意到我的代码中的错误并知道发生了什么?
当你使用Collection Type时,你需要额外编写html & js,来处理这种类型的字段。更多内容在 Symfony 文档中:http://symfony.com/doc/current/reference/forms/types/collection.html