有一个实体 myEntity,其记录属性为 json 类型,反序列化后如下所示:
class myEntity
{
public function getRecords()
{
return [
['param1' => 'value1', 'param2' => 'value2'],
['param1' => 'value1', 'param2' => 'value2'],
];
}
}
我想创建一个将用这些反序列化值填充的表单,并为新值多留一个“行”:
MAIN FORM
-> Record #1
-> Param 1
-> Param 2
-> Record #2
-> Param 1
-> Param 2
-> New Record <- пустая строка для новых значений
-> Param 1
-> Param 2
我在控制器中创建了一个新表单:
$this->container->get('form.factory')->create(myMainForm::class, $myEntity);
在主窗体中,我创建了一个嵌套records
:
class myMainFormType extends EasyAdminFormType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('records', myRecordsType::class);
}
}
而这里我有问题,不知道怎么遍历所有现有的数组值
class myRecordsType extends EasyAdminFormType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// TODO buildForm(), something like:
// foreach (... as ...)
// {
// $builder->add('record', myRecordType::class);
// }
}
}
最后,将创建文本字段本身,这里一切都很清楚:
class myRecordType extends EasyAdminFormType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('param1', TextType::class);
$builder->add('param2', TextType::class);
}
}