如何在Symfony 4中手动创建嵌套表单并用数组填充它

时间:2019-02-18 09:31:18

标签: forms symfony4

我有一个实体@echo off set "inputfile=install.txt" set /p "_strfind=string to replace? " set /p "_strinsert=replacement string? " set /p "_strfind2=enter Executable numeric value to replace? " set /p "_strinsert2=enter the replacement value for executable? " set "_strfind2=%_strfind2%.exe" set "_strinsert2=%_strinsert2%.exe" for /f "tokens=*" %%a in ('type "%inputfile%" ^| find /v /n "" ^& break ^> "%inputfile%"') do ( set "str=%%a" setlocal enabledelayedexpansion set "str=!str:%_strfind%=%_strinsert%!" set "str=!str:%_strfind2%=%_strinsert2%!" call set "str=%%str:*]=%%" >>%inputfile% echo(!str! endlocal ) ,它的json类型属性名为myEntity,在反序列化后看起来像这样:

records

我想创建一个将用这些反序列化值填充的表单,并为新值再添加一行:

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 <- additional empty row
        -> Param 1
        -> Param 2

在主表单中,我创建一个名为$this->container->get('form.factory')->create(myMainForm::class, $myEntity); 的嵌套表单:

records

这里有个问题,我不知道如何遍历数组的所有现有值(并添加其他行),因为$ builder-> getData()为空:

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);
        // }
    }
}

0 个答案:

没有答案