我使用symfony表单来处理API请求。我有事件实体和EventForm类。我需要创建用于一次创建多个事件的批量API。如何重用现有表单来创建多个事件实体?请求将如下所示:
{
"events": [
{
"title": "Event One",
"description": "",
"startsAt": "2019-08-16T16:00:00+0300",
"endsAt": "2019-08-16T17:00:00+0300",
"allDay": false
},
{
"title": "Event Two",
"description": "",
"startsAt": "2019-08-16T16:00:00+0300",
"endsAt": "2019-08-16T17:00:00+0300",
"allDay": false
}
]
}
我读到有关嵌入https://symfony.com/doc/3.4/form/form_collections.html的表单的信息,似乎不是我的情况,因为我不需要为父实体设置多个子代,而只需创建多个事件即可。
这是我的EventForm类:
class EventForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('title', TextType::class, [
'required' => true,
])
->add('description', TextType::class, [
'required' => false,
'empty_data' => '',
])
->add('startsAt', DateTimeType::class, [
'required' => true,
'widget' => 'single_text',
])
->add('endsAt', DateTimeType::class, [
'required' => true,
'widget' => 'single_text',
])
->add('allDay', CheckboxType::class, [
'required' => true,
])
;
}
}
我创建BulkEventForm类:
class BulkEventForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('events', CollectionType::class, [
'entry_type' => EventForm::class
])
;
}
}
我希望我得到事件的数组或集合,但是在处理了请求表单之后,数据是:
[
'events' => []
]
是否可以在不循环请求和创建多个表单的情况下获得预期的结果?