【发布时间】:2020-10-23 09:12:23
【问题描述】:
在我的项目中,我希望能够在表单上添加一个集合。我想到了 FormTypeCollection。但问题是,我需要这样的东西: 表单末尾有一个“新建”按钮,每次单击新建时,都会添加一个“迷你表单”,您需要填写三个输入:“名称、文本、链接”。例如,我希望它以艺术家 = [名称、文本、链接] 的形式存储在数据库中。我不知道该怎么做。我不想添加实体艺术家,因为我只需要它来显示,我不需要将它作为实体存储在数据库中。 我现在的代码是这样的:
protected function configureFormFields(FormMapper $formMapper)
{
$formMapper
->with('Contenu')
->add('published', CheckboxType::class, ['required' => false, 'label' => 'Publier'])
->add('title', TextType::class, ['required' => true, 'label' => 'Titre'])
->add('marketingEtiquette', TextType::class, ['required' => false, 'label' => 'Etiquette Marketing'])
->add('textLink', TextType::class, ['required' => true, 'label' => 'Texte du lien'])
->add('shoppingLink', TextType::class, ['required' => true, 'label' => 'Lien'])
->add('media', ElFinderType::class, array(
'label' => 'Photo',
'instance' => 'form',
'enable' => true,
'required' => true,
'attr' => array('class' => 'form-control')
)
)
->add('position',ChoiceType::class, array(
'label' => 'Position dans la page',
'choices' => array(
'Bloc Artistes' => 'artists',
'Bloc haut de page' => 'top',
'Bloc bas de page' => 'bottom'
)
))
->add('artists',CollectionType::class,array(
'label' => 'Les artistes',
'allow_add' => true,
))
->end();
}
我不知道如何向字段艺术家添加 3 个字段并在单击添加按钮时生成它们。我什至不知道这是否可能。我也不知道数据库中的“艺术家”类型应该是什么。
编辑: 我想做类似的事情,所以我不需要创建实体或 FormType:
->add('artists',CollectionType::class,array(
'entry_type' => TextType::class ,
'entry_options' => [
'artistName' => TextType::class,
'artistText' => TextType::class,
'artistLink' => TextType::class,
],
'label' => 'Les artistes',
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'by_reference' => false
))
但它不起作用,所以我想我不能。错误:
The current field `artists` is not linked to an admin. Please create one for the target entity : ``
编辑 2: 我创建了我的 ArtistFormType:
class ArtistFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('artistName', TextType::class, array(
'label' => 'Nom de l\'artiste'
))
->add('artistText', TextType::class, array(
'label' => 'Texte sous l\'artiste'
))
->add('artistLink', TextType::class, array(
'label' => 'Lien vers l\'artiste'
))
;
}
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults([
'data_class' => null,
]);
}
}
我是这样称呼它的:
->add('artists',CollectionType::class,array(
'entry_type' => ArtistFormType::class,
'label' => 'Les artistes',
'allow_add' => true,
'allow_delete' => true,
'delete_empty' => true,
'by_reference' => false
))
但我得到了同样的错误:
The current field `artists` is not linked to an admin. Please create one for the target entity : ``
【问题讨论】: