我正在尝试将表单嵌入到表单中。就我而言:我想将 Period 和 Price 表单嵌入到要约表单中的 Poi 表单中。架构 :
关系:
实体。
几天来我一直在寻找解决方案,我真的需要帮助,所以如果有人可以帮助我,那就太好了。
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/