【问题标题】:How to fill Smyfony CollectionType with N forms based on Database Rows如何使用基于数据库行的 N 个表单填充 Smyfony CollectionType
【发布时间】:2019-07-26 08:01:31
【问题描述】:

我有一个我创建的 EditAnnouncementType 类型表单的 CollectionType。此 CollectionType 将用于呈现表单以处理用户编辑公告的某些文本,其中每个公告都有自己的编辑模式打开(模式具有唯一的 ID)

$editForm = $this->createFormBuilder()
        ->add('editForms', CollectionType::class,
            [
                'entry_type' => EditAnnouncementType::class,
                'allow_add' => true,
                'prototype' => true,
                'by_reference' => false,
                'required' => false,
        ])
        ->add('edit', SubmitType::class,
            array
            (
                'label' => 'Save changes',
                'attr' => ['class' => 'btn btn-primary']
            ))
        ->setData($this->getDoctrine()->getRepository(Announcement::class)->findAll())
        ->getForm()
        ;

如何根据 N 行(又名 N 个公告实体)预填 N 个表单。

表单类型代码

class EditAnnouncementType extends AbstractType

{ /** * @param FormBuilderInterface $builder * @param 数组 $options */ 公共函数 buildForm(FormBuilderInterface $builder, array $options) { $建造者 ->add('edit', SubmitType::class, 大批 ( '标签' => '保存更改', 'attr' => ['class' => 'btn btn-primary'] )) ->add('id', HiddenType::class, []) ; }

/**
 * Returns the name of this type.
 *
 * @return string
 */
public function getName()
{
    return 'edit_announcement';
}

}

【问题讨论】:

    标签: php symfony


    【解决方案1】:

    试试这个

    $editForm = $this->createFormBuilder()
            ->add('editForms', CollectionType::class,
                [
                    'entry_type' => EditAnnouncementType::class,
                    'allow_add' => true,
                    'prototype' => true,
                    'by_reference' => false,
                    'required' => false,
            ])
            ->add('edit', SubmitType::class,
                array
                (
                    'label' => 'Save changes',
                    'attr' => ['class' => 'btn btn-primary']
                ))
            ->setData(['editForms' => $this->getDoctrine()->getRepository(Announcement::class)->findAll()])
            ->getForm()
            ;
    

    【讨论】:

    • 那行不通。收到关于 Announcement 实体不允许 ArrayAccess 用于我的 FormType 中的“id”的错误。包含表单类型的代码
    • 可能你没理解对,集合是用来做什么的。集合将用于一次保存多个数据集。这意味着,您的设计是错误的,因为您不想一次编辑所有模式。您应该只加载一个模式来编辑一个公告。最好的方法是使用 ajax 加载 modalcontent 并将内容推送到 modalbody 中,然后显示 modal。
    猜你喜欢
    • 1970-01-01
    • 2022-01-04
    • 1970-01-01
    • 2016-09-01
    • 2015-10-28
    • 2019-06-21
    • 2016-03-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多