There is an entity myEntity with the records property of the json type, which after deserialization looks like this:

class myEntity { public function getRecords() { return [ ['param1' => 'value1', 'param2' => 'value2'], ['param1' => 'value1', 'param2' => 'value2'], ]; } } 

I want to create a form that will be filled with these deserialized values, and leave another “string” for new values:

 MAIN FORM -> Record #1 -> Param 1 -> Param 2 -> Record #2 -> Param 1 -> Param 2 -> New Record <- пустая строка для новых значений -> Param 1 -> Param 2 

I create a new form in the controller:

 $this->container->get('form.factory')->create(myMainForm::class, $myEntity); 

In the main form I create nested records :

 class myMainFormType extends EasyAdminFormType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('records', myRecordsType::class); } } 

And here I have problems, I don’t know how I can iterate over all existing values ​​of the array (and add one more extra), $ builder-> getData () is empty:

 class myRecordsType extends EasyAdminFormType { public function buildForm(FormBuilderInterface $builder, array $options) { // TODO buildForm(), something like: // foreach (... as ...) // { // $builder->add('record', myRecordType::class); // } } } 

And in the end, the text fields themselves will be created, then everything is clear:

 class myRecordType extends EasyAdminFormType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->add('param1', TextType::class); $builder->add('param2', TextType::class); } } 

    2 answers 2

    getData is a method of the FormEvent class. I assume that you need to hang an event, and call getData in it.

     public function buildForm(FormBuilderInterface $builder, array $options) { ... $builder->addEventListener(FormEvents::PRE_SET_DATA /*POST_SUBMIT ...*/, function (FormEvent $event) { $data = $event->getData(); //и тут уже перебор }); } 

    Events that are in Form Events .

      Thanks to the help of @ kostiantyn-okhotnyk, it turned out like this:

       class myRecordsType extends EasyAdminFormType { public function buildForm(FormBuilderInterface $builder, array $options) { $builder->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event) { $form = $event->getForm(); foreach ($event->getData() as $i => $d) { $form->add($i, MyRecordType::class); $form->get($i)->setData($d); } $i = isset($i) ? $i++ : 0; $form->add($i, MyRecordType::class); }); } }