首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >EasyAdmin 3:嵌套表单

EasyAdmin 3:嵌套表单
EN

Stack Overflow用户
提问于 2020-07-17 11:34:47
回答 2查看 8K关注 0票数 6

我试图将表单嵌入到表单中。在我的例子中:我想将期限和价格表嵌入到Poi表单中。架构:

form

  • Poi form
    • form
      • form
      • Price

关系:

  • Poi实体与报价实体有关系OneToMany,
  • 提供实体与价格实体有关系,ManyToMany与周期实体有关系。

几天来我一直在寻找解决方案,我真的需要帮助,所以如果有人能帮我,那就太好了。

1.第一次测试: CollectionField在CollectionField PoiCrudController中的使用:

代码语言:javascript
复制
public function configureFields(string $pageName): iterable {
    $offers = CollectionField::new('offers')
            ->setFormTypeOptions([
                'delete_empty' => true,
                'by_reference' => false,
            ])
            ->setEntryIsComplex(false)
            ->setCustomOptions([
                'allowAdd' => true,
                'allowDelete' => true,
                'entryType' => 'App\Form\OfferType',
                'showEntryLabel' => false,
            ]),

在OfferType中:

代码语言:javascript
复制
class OfferType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
    
        $builder
            ->add('description', CollectionType::class, array(
                'allow_add' => true,
                'allow_delete' => true,
                'delete_empty' => true,
                'by_reference' => false,
                'entry_type' => TextEditorType::class,
                'entry_options' => [
                  'label' => false,
                ],
                'label' => 'Description',
              ))

            ->add('createdAt')
            ->add('updatedAt')
            ->add('periods')
            ->add('poi')
        ;
    }
}

错误消息=> "App\ entity \Poi“实体有一个repositoryClass设置为"App\Entity\PoiRepository",但这不是一个有效的类。检查你的班级名称。如果这是服务id,请确保此服务存在并使用"doctrine.repository_service".进行标记。

如果我将'entryType' => 'App\Form\OfferType',替换为'entryType' => 'App\Form\PoiType' in PoiCrudController,并在PoiType中添加以下代码:

代码语言:javascript
复制
class PoiType extends AbstractType {

    public function buildForm(FormBuilderInterface $builder, array $options) {
    
        $builder
            ->add('offers', CollectionType::class, array(
                'allow_add' => true,
                'allow_delete' => true,
                'delete_empty' => true,
                'by_reference' => false,
                'entry_type' => TextType::class, // cette ligne pose problème
                'entry_options' => [
                  'label' => false,
                ],
                'label' => 'Offres',
              ))

然后将Poi表单嵌套到Poi表单中,在其中出现字段“要约”。如果我将'entry_type' => TextType::class替换为'entry_type' => TextEditorType::class,,将出现一个新的错误:

错误消息:不可能访问空变量上的属性("customOptions")。在vendor\easycorp\easyadmin-bundle\src\Resources\views\crud\form_theme.html.twig中(第424行) {% set numOfRows = form.vars.ea_crud_form.ea_field.customOptions.get('numOfRows') %}

2.第二次测试: CollectionField的使用

在PoiCrudController中:

代码语言:javascript
复制
    CollectionField::new('offers', 'Offres')
                ->allowAdd() 
                ->allowDelete()
                ->setEntryIsComplex(true)
                ->setEntryType(OfferCrudController::class)
            ->setFormTypeOptions([
                'by_reference' => 'false' 
            ]),

"Symfony\Component\Form\FormTypeInterface.错误消息=>无法加载类型"App\Controller\Admin\OfferCrudController":类不实现 My实现AbstractType。

3.第三个测试: AssociationField的使用

在PoiCrudController中:

代码语言:javascript
复制
    AssociationField::new('offers')
                ->setFormTypeOptions([
                    'by_reference' => false,
                    'multiple' => true,
                    'allow_add' => true
                ]),

解决表单"Symfony\Bridge\Doctrine\Form\Type\EntityType":选项的错误--选项"allow_add“不存在 =>Issue #3528 https://github.com/EasyCorp/EasyAdminBundle/issues/3528

EN

回答 2

Stack Overflow用户

发布于 2020-10-20 08:37:34

我自己在这个问题上挣扎了很长时间,我终于找到了一个解决办法。

我选择了第一个测试路径,但是使用了第二个测试属性:

CollectionField创建保持不变,只需将OfferType表单类型设置为OfferCrudController,而不是将OfferCrudController设置为EntryType

代码语言:javascript
复制
    CollectionField::new('offers', 'Offres')
            ->allowAdd() 
            ->allowDelete()
            ->setEntryIsComplex(true)
            ->setEntryType(OfferType::class)
        ->setFormTypeOptions([
            'by_reference' => 'false' 
        ]),

然后编辑OfferType

代码语言:javascript
复制
class OfferType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add(...) // whatever you want
        ;
    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults([
            'data_class' => Offer::class,

        ]);
    }
}

添加configureOptions方法为我解决了这个问题。

票数 9
EN

Stack Overflow用户

发布于 2020-08-13 09:55:15

“允许添加”应该在“集合”字段中,而不是在AssociationField中。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62953123

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档