title: Symfony 2 Form error "This form should not contain extra fields." when submitting a form
body: <p>I am submitting a form and handling it in the Sylius <code>ResourceController</code> which submits the form and validates it.</p>
  
  <p>This is the form in situ:</p>
  
  <pre><code>&lt;tr&gt;
     &lt;form method="post" action="{{ path('backend_course_row_update', {
   'courseeId' : course.id, 'id' : row.id }) }}" novalidate&gt;
       {{ form_widget(form.channel) }}
       {{ form_widget(form.name) }}
       {% for size in form.sizes %}
          {{ form_row(size) }}
       {% endfor %}
       {{ form_row(form._token) }}
       &lt;td align="right" style="width: 140px;"&gt;
           &lt;button class="btn btn-primary" type="submit"&gt;
              &lt;i class="glyphicon glyphicon-save"&gt;&lt;/i&gt;Save
           &lt;/button&gt;
       &lt;/td&gt;
    &lt;/form&gt;
  &lt;/tr&gt;
  </code></pre>
  
  <p>The "form" here is a <code>CourseGuideRowType</code>, which looks like this:</p>
  
  <pre><code>/**
   * {@inheritdoc}
   */
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      $builder
          -&gt;add('channel', 'channel_choice', array(
              'required'    =&gt; false
          ))
          -&gt;add('name', 'text')
          -&gt;add('sizes', 'course_guide_row_sizes', array('numColumns' =&gt; $options['numColumns']))
      ;
  }
  </code></pre>
  
  <p><code>CourseGuideRowSizesType</code> then looks like this:</p>
  
  <pre><code>/**
   * {@inheritdoc}
   */
  public function buildForm(FormBuilderInterface $builder, array $options)
  {
      for ($i = 0; $i &lt; $options['numColumns']; $i++) {
          $builder-&gt;add($i, 'text', array('required' =&gt; 'false'));
      }
      $builder-&gt;addEventListener(
          FormEvents::PRE_SUBMIT,
          function (FormEvent $event) use ($options) {
              $form = $event-&gt;getForm();
              for ($i = 0; $i &lt; $options['numColumns']; $i++) {
                  if (empty($form-&gt;get($i)-&gt;getData())) {
                      $form-&gt;remove($i, 'text');
                  }
              }
          }
      );
  }
  </code></pre>
  
  <p>However, when I submit the form and dump out the errors like this:</p>
  
  <p><code>$form-&gt;submit($request, !$request-&gt;isMethod('PATCH'))-&gt;getErrors()</code></p>
  
  <p>I get:</p>
  
  <pre><code>"This form should not contain extra fields."
        #messageParameters: array:1 [▼
          "{{ extra_fields }}" =&gt; "0", "1", "2", "3", "4", "5"
        ]
          -extraData: array:6 [▼
            0 =&gt; "36"
            1 =&gt; "38"
            2 =&gt; "40"
            3 =&gt; "42"
            4 =&gt; "44"
            5 =&gt; "46"
          ]
  </code></pre>
  
  <p>The "extra data" is the "sizes" fields.</p>
  
  <p>Am I doing something blatantly wrong here?</p>
  
  <p><strong>EDIT June 2017: You can now use 'allow_extra_fields' to suppress this error. <a href="http://symfony.com/doc/current/reference/forms/types/form.html#allow-extra-fields" rel="nofollow noreferrer">http://symfony.com/doc/current/reference/forms/types/form.html#allow-extra-fields</a></strong></p>