【问题标题】:EasyAdmin 3 : Nested formsEasyAdmin 3:嵌套表单
【发布时间】:2020-11-07 05:02:15
【问题描述】:

我正在尝试将表单嵌入到表单中。在我的情况下:我想将 Period 和 Price 表单嵌入到 Offer 表单到 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,
            ]),

在 OfferType 中:

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 表单嵌套到出现“offer”字段的 Poi 表单中。 如果我将'entry_type' => TextType::class 替换为'entry_type' => TextEditorType::class,,则会出现一个新错误:

错误消息:无法访问空变量的属性(“customOptions”)。 在 vendor\easycorp\easyadmin-bundle\src\Resources\views\crud\form_theme.html.twig 中(第 424 行) {% 设置 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
                ]),

ERROR MESSAGE => 解析“Symfony\Bridge\Doctrine\Form\Type\EntityType”形式的选项时发生错误:选项“allow_add”不存在 =>问题#3528 [https://github.com/EasyCorp/EasyAdminBundle/issues/3528][2]

【问题讨论】:

    标签: symfony4 easyadmin


    【解决方案1】:

    我自己也为这个问题苦苦挣扎了很久,终于找到了解决办法。

    我选择了Fist test途径,但使用Second test属性:

    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 方法为我解决了这个问题。

    【讨论】:

      【解决方案2】:

      允许添加应该在集合字段中,而不是在关联字段中。

      【讨论】:

      • @MoubarZakaria 请避免给出单行答案,您可以在评论框中发表。
      猜你喜欢
      • 2021-07-09
      • 2022-01-11
      • 2021-08-31
      • 2021-09-01
      • 1970-01-01
      • 2017-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多