Laz*_*Lez 5 symfony4 easyadmin
我正在尝试将表单嵌入到表单中。就我而言:我想将 Period 和 Price 表单嵌入到 Offer 表单中的 Poi 表单中。架构 :
关系:
几天来我一直在寻找解决方案,我真的需要帮助,所以如果有人可以帮助我,那就太好了。
1. 第一个测试: 在我的 PoiCrudController 中使用 CollectionField:
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,
]),
Run Code Online (Sandbox Code Playgroud)
在优惠类型中:
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')
;
}
}
Run Code Online (Sandbox Code Playgroud)
错误消息 => “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',
))
Run Code Online (Sandbox Code Playgroud)
然后将 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'
]),
Run Code Online (Sandbox Code Playgroud)
错误消息 => 无法加载类型“App\Controller\Admin\OfferCrudController”:类没有实现“Symfony\Component\Form\FormTypeInterface。 我的表单实现了 AbstractType 所以...
3. 第三个测试:AssociationField 的使用
在 PoiCrudController 中:
AssociationField::new('offers')
->setFormTypeOptions([
'by_reference' => false,
'multiple' => true,
'allow_add' => true
]),
Run Code Online (Sandbox Code Playgroud)
错误消息 => 解析“Symfony\Bridge\Doctrine\Form\Type\EntityType”形式的选项时发生错误:选项“allow_add”不存在 => 问题 #3528 [https://github.com/ EasyCorp/EasyAdminBundle/issues/3528][2]
小智 5
我自己一直在为这个问题苦苦挣扎,我终于找到了解决方案。
我选择了Fist 测试路径,但使用了Second 测试属性:
创建的 CollectionField 保持不变,只是将OfferType表单类型设置OfferCrudController为EntryType.
CollectionField::new('offers', 'Offres')
->allowAdd()
->allowDelete()
->setEntryIsComplex(true)
->setEntryType(OfferType::class)
->setFormTypeOptions([
'by_reference' => 'false'
]),
Run Code Online (Sandbox Code Playgroud)
然后编辑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,
]);
}
}
Run Code Online (Sandbox Code Playgroud)
添加 configureOptions 方法为我解决了这个问题。
| 归档时间: |
|
| 查看次数: |
4754 次 |
| 最近记录: |