title: POST array data in twig alongside form data in symfony 3
body: <p>I have a web app that is importing user emails and names from a .csv file into the database for a mail merge.</p>
  
  <p>After the upload I am passing the .csv data (as an array) from the controller to the twig to be column matched to make sure the correct values are being persisted to the database (firstname column is actually firstname etc). The user verifies this with a choiceType dropdown above the columns and their choice is submitted via a button built into the form. This is then taken by the controller and used to persist the correct column data to the database. However, since the .csv file is stored temporarily after the request/response cycle it is purged. I therefore want to POST the .csv data I have sent to be displayed in the twig back to the controller to be persisted to the database. </p>
  
  <p>Can this only be done using javascript or is there a way to do this in Symfony?</p>
  
  <p>Here is the relevant part of the twig:</p>
  
  <pre><code>        &lt;div class="row"&gt;
          &lt;div class="col-lg-12"&gt;                
              &lt;section class="panel"&gt;
                  &lt;div class="panel-body"&gt;
                      &lt;table class='table table-hover no-margin reports-table'&gt;
                          &lt;thead&gt;
                              &lt;tr&gt;
                                  &lt;th&gt;
                                     {{ form_start(emailCampaignListUserImportCheckForm)}}
                                     {{ form_row(emailCampaignListUserImportCheckForm.column1) }}
                                  &lt;/th&gt;
                                     &lt;th&gt;
                                        {{ form_start(emailCampaignListUserImportCheckForm)}}
                                        {{ form_row(emailCampaignListUserImportCheckForm.column2) }}
                                     &lt;/th&gt;
                                        &lt;th&gt;
                                          {{ form_start(emailCampaignListUserImportCheckForm)}}
                                          {{ form_row(emailCampaignListUserImportCheckForm.column3) }}
                                        &lt;/th&gt;
                                         &lt;th class='entry-actions' data-hide="phone,tablet" data-name="Actions"&gt;&lt;/th&gt;
                              &lt;/tr&gt;
                          &lt;/thead&gt;
  
                          &lt;tbody&gt;
                                {% for key in addedUsers|slice(1)  %}  
                                  &lt;tr&gt;
                                      &lt;td&gt;
                                             {{key[0]}} 
                                          &lt;div class="row-options"&gt;
                                              &lt;span class="available-options" style="display: none;"&gt;
                                                  &lt;div class="options"&gt;                                                                                                                     
                                                      &lt;a class="delete-entry"&gt;
                                                          &lt;i class="fa fa-fw fa-trash-o"&gt;&lt;/i&gt; Delete
                                                      &lt;/a&gt;                                                        
                                                  &lt;/div&gt;
                                              &lt;/span&gt;
                                          &lt;/div&gt;
                                      &lt;/td&gt;
                                      &lt;td&gt;
                                        {{key[1]}}                                          
                                      &lt;/td&gt; 
                                          &lt;td&gt;                                       
                                             {{key[2]}}                                            
                                          &lt;/td&gt;
                                              &lt;td&gt;
                                                  &lt;div class="btn-group"&gt;
                                                      &lt;button type="button" class="btn btn-info btn-xs dropdown-toggle" data-toggle="dropdown"&gt;
                                                          &lt;i class="fa fa-fw fa-cog"&gt;&lt;/i&gt; &lt;i class="fa fa-fw fa-caret-right"&gt;&lt;/i&gt;
                                                      &lt;/button&gt;
                                              &lt;ul class="dropdown-menu" role="menu"&gt;                                                   
                                                  &lt;li class="delete-link"&gt;
                                                      &lt;a&gt;
                                                          &lt;i class="fa fa-fw fa-trash-o"&gt;&lt;/i&gt; Delete
                                                      &lt;/a&gt;
                                                  &lt;/li&gt;
                                                  &lt;li class="delete-options"&gt;
                                                      &lt;a class="cancel"&gt;
                                                          &lt;i class="fa fa-fw fa-times"&gt;&lt;/i&gt; Cancel
                                                      &lt;/a&gt;
                                                  &lt;/li&gt;
                                                  &lt;li class="delete-options"&gt;
                                                      &lt;a class="delete" href="#"&gt;
                                                          &lt;i class="fa fa-fw fa-trash-o"&gt;&lt;/i&gt; Confirm Delete
                                                      &lt;/a&gt;
                                                  &lt;/li&gt;
                                              &lt;/ul&gt;
                                          &lt;/div&gt;                                        
                                      &lt;/td&gt;
                                  &lt;/tr&gt;                                     
                                {% endfor %}
                          &lt;/tbody&gt;
                      &lt;/table&gt;                          
                  &lt;/div&gt;   
  </code></pre>
  
  <p>Here is the form:</p>
  
  <pre><code>    class EmailCampaignListUserImportCheckForm extends AbstractForm
  {
  
      public function buildForm(FormBuilderInterface $builder, array $options)
      {
  
          $builder
  
              -&gt;add(
                  'column1',
                  ChoiceType::class,
                  [   
                      'choices'  =&gt; [
  
                          'Email' =&gt; 'email',
                          'First Name' =&gt; 'firstname',
                          'Surname' =&gt; 'surname'
                      ]
                  ]
              )
  
              -&gt;add(
                  'column2',
                  ChoiceType::class,
                  [   
                      'choices'  =&gt; [
  
                          'First Name' =&gt; 'firstname',
                          'Surname' =&gt; 'surname',
                          'Email' =&gt; 'email'
                      ]
                  ]
              )
  
              -&gt;add(
                  'column3',
                  ChoiceType::class,
                  [   
                      'choices'  =&gt; [
  
                          'Surname' =&gt; 'surname',
                          'Email' =&gt; 'email',
                          'First Name' =&gt; 'firstname'
  
                      ]
                  ]
              )
  
              -&gt;add(
                  'submit',
                  SubmitType::class,
                  [
                      'label' =&gt;  'Save'
                  ]
              );
  
  
      }
  </code></pre>
  
  <p>Here is how the file is uploaded:</p>
  
  <pre><code> //  grab uploaded .csv file
      $file = $_FILES["file"]["tmp_name"];
      $fileimplode = implode("/", $file); 
  
      // convert .csv if made on mac
      if (!ini_get("auto_detect_line_endings")) {
          ini_set("auto_detect_line_endings", '1');
      }
      //  create array from .csv
      $reader = Reader::createFromPath($fileimplode);
      $addedUsers = $reader-&gt;fetchAll();
      $emailCampaignList = $this-&gt;get('email.list_repository')-&gt;find($request-&gt;get('listId'));
  
      return $this-&gt;render('EmailBundle:MailMerge:emailCampaignListsUsersImportCheck.html.twig',
      [
          'emailCampaignList' =&gt; $emailCampaignList,
          'addedUsers' =&gt; $addedUsers,
          'emailCampaignListUserImportCheckForm' =&gt; $emailCampaignListUserImportCheckForm-&gt;createView(),
          'listId' =&gt; $request-&gt;get('listId')
      ]                   
      );
  </code></pre>