php - 如何使用基于数据库行的 N 个表单填充 Smyfony CollectionType
问题描述
我有一个我创建的 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 array $options */ public function buildForm(FormBuilderInterface $builder, array $options) { $builder ->add('edit', SubmitType::class, array ( 'label ' => '保存更改', 'attr' => ['class' => 'btn btn-primary'] )) ->add('id', HiddenType::class, []) ; }
/**
* Returns the name of this type.
*
* @return string
*/
public function getName()
{
return 'edit_announcement';
}
}
解决方案
试试这个
$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()
;
推荐阅读
- python - 如何使 Python LightGBM 代码接受列表
- python - Pandas groupby 中的计数频率
- kubernetes - 为 Kubernetes 作业提供文件
- python - 将当前元素与列表中的任何元素进行比较
- sql - 将现有标题放在多行的一行上
- ios - Xcode 编译器错误:编译器无法在合理的时间内对该表达式进行类型检查(Xcode 12.0 SwiftUI)
- android - 在 Android Q 中以编程方式连接到 WiFi
- laravel - 当我搜索产品时,laravel 路由路径会发生变化
- css - 使用 css 和 react 的所有转换属性是什么意思?
- php - reCaptcha v2“我不是机器人”突然坏了,没有得到“g-recaptcha-response”