symfony4 - EasyAdmin 3 : Nested forms

标签 symfony4 easyadmin

我正在尝试将表单嵌入到表单中。就我而言:我想将 Period 和 Price 表单嵌入到要约表单中的 Poi 表单中。架构 :

  • Poi 形式
  • 报价单
  • 价格表
  • 期间形式



  • 关系:
  • Poi 实体与 Offer 实体
  • 有 OneToMany 关系
  • Offer 实体有 OneToMany 和 Price 实体,ManyToMany 有 Period
    实体。

  • 几天来我一直在寻找解决方案,我真的需要帮助,所以如果有人可以帮助我,那就太好了。
    1. 第一个测试:CollectionField 的使用
    在我的 PoiCrudController 中:
    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,
                ]),
    
    在优惠类型中:
    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 中添加此代码:
    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 表单中,其中出现“offer”字段。
    如果我用 '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 中:
        CollectionField::new('offers', 'Offres')
                    ->allowAdd() 
                    ->allowDelete()
                    ->setEntryIsComplex(true)
                    ->setEntryType(OfferCrudController::class)
                ->setFormTypeOptions([
                    'by_reference' => 'false' 
                ]),
    
    错误消息 => 无法加载类型“App\Controller\Admin\OfferCrudController”:类没有实现“Symfony\Component\Form\FormTypeInterface。
    我的表单实现了 AbstractType 所以...
    3. 第三个测试:AssociationField 的使用
    在 PoiCrudController 中:
        AssociationField::new('offers')
                    ->setFormTypeOptions([
                        'by_reference' => false,
                        'multiple' => true,
                        'allow_add' => true
                    ]),
    
    错误消息 => 解析“Symfony\Bridge\Doctrine\Form\Type\EntityType”形式的选项时发生错误:选项“allow_add”不存在
    => 问题 #3528 [https://github.com/EasyCorp/EasyAdminBundle/issues/3528][2]

    最佳答案

    我自己也在这个问题上挣扎了很长时间,我终于找到了解决方案。
    我选择了 Fist 测试 途径,但使用 第二个测试 属性:
    创建的 CollectionField 保持不变,只是将 OfferType 表单类型设置为 OfferCrudController 而不是 EntryType

        CollectionField::new('offers', 'Offres')
                ->allowAdd() 
                ->allowDelete()
                ->setEntryIsComplex(true)
                ->setEntryType(OfferType::class)
            ->setFormTypeOptions([
                'by_reference' => 'false' 
            ]),
    
    然后编辑 OfferType :
    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 方法为我解决了这个问题。

    关于symfony4 - EasyAdmin 3 : Nested forms,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62953123/

    相关文章:

    symfony - Easy Admin 3 (Symfony 4) OneToOne 关系中的 AssociationField 显示已经关联的实体

    php - 如何在 Symfony 5 上使用带有 easyAdmin 3 的 Vich uploader

    php - 使用 `must implement interface Doctrine\ORM\EntityManagerInterface error` 将 EntityManagerInterface 注入(inject)我的服务

    symfony - 如何从api平台返回自定义数据?

    symfony - Symfony 4:如何组织文件夹结构(即您的业务逻辑)

    symfony - easyadmin symfony Doctrine 中的列表数组/集合问题

    CKEditor 不显示与 easyadmin 集成

    Symfony 4 服务在资源文件中声明时被忽略

    php - Symfony 4 - 自动加载器期望在文件中定义类 [...]

    添加: The Doctrine type of the . ...时出现Symfony EasyAdmin 3.x ManyToMany错误...字段为 "4",EasyAdmin尚不支持