我试图将表单嵌入到表单中。在我的例子中:我想将期限和价格表嵌入到Poi表单中。架构:
form
关系:
几天来我一直在寻找解决方案,我真的需要帮助,所以如果有人能帮我,那就太好了。
1.第一次测试: CollectionField在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表单嵌套到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中:
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中:
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
发布于 2020-10-20 08:37:34
我自己在这个问题上挣扎了很长时间,我终于找到了一个解决办法。
我选择了第一个测试路径,但是使用了第二个测试属性:
CollectionField创建保持不变,只需将OfferType表单类型设置为OfferCrudController,而不是将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方法为我解决了这个问题。
发布于 2020-08-13 09:55:15
“允许添加”应该在“集合”字段中,而不是在AssociationField中。
https://stackoverflow.com/questions/62953123
复制相似问题