How to get a Symfony Embedded Dynamic Form Unmapped data

33 views Asked by At

I have a dynamic form that is generated dynamically like the following.

<input type="hidden" id="project_teams_18_cropped_file_key" name="project[teams][1][cropped_file_key]" class="cropped_file_key">
<input type="hidden" id="project_teams_18_cropped_file_key" name="project[teams][2][cropped_file_key]" class="cropped_file_key">
<input type="hidden" id="project_teams_18_cropped_file_key" name="project[teams][8][cropped_file_key]" class="cropped_file_key">
.....
<input type="hidden" id="project_teams_18_cropped_file_key" name="project[teams][dynamic_key][cropped_file_key]" class="cropped_file_key">

Form Type:

->add('cropped_file_key', HiddenType::class, [
    'mapped' => false
])

Since the form inputs key are in random order I cannot do foreach($project->getTeams() as $key => $value){} because $key runs in a sequence mannerr.

I tried like $form->get('cropped_file_key') it didn't work and it asks for the key. What is the best way to get the value of such dynamic form inputs?

1

There are 1 answers

2
Skunka On

Is your FormType is your parent form or is it a child form with type of CollectionType ?

If it is a child form, as mentonned in this post Access Embedded Form Data, you may try the following:

foreach ($project->getTeams() as $key => $coordinator) { 
    $coordinatorImage = $teamImageUpload->uploadTeamImage(
            $form->get('teams')[$key]['cropped_file']->getData(), 
            $coordinator
    ); 
    $coordinator->setImage($coordinatorImage); 
}

This will allow you to run through your collection and see what they contain`.

Otherwise, you should consider using Embedded collection of Forms from Symfony docs.