Symfony 2 This form should not contain extra fields

19,123

Solution 1

It seems to me that you have the problem because of the token field. If it is so, try to add options to createFormBuilder():

$this->createFormBuilder($search, array(
        'csrf_protection' => true,
        'csrf_field_name' => '_token',
    ))
    ->add('searchinput', 'text', array('label'=>false, 'required' =>false))
    ->add('search', 'submit')
    ->getForm();

To find out the extra field use this code in controller, where you get the request:

$data = $request->request->all();

print("REQUEST DATA<br/>");
foreach ($data as $k => $d) {
    print("$k: <pre>"); print_r($d); print("</pre>");
}

$children = $form->all();

print("<br/>FORM CHILDREN<br/>");
foreach ($children as $ch) {
    print($ch->getName() . "<br/>");
}

$data = array_diff_key($data, $children);
//$data contains now extra fields

print("<br/>DIFF DATA<br/>");
foreach ($data as $k => $d) {
    print("$k: <pre>"); print_r($d); print("</pre>");
}

$form->bind($data);

Solution 2

This message is also possible if you added/changed fields in your createFormBuilder() and press refresh in your browser...

In this case it's ok after sending the form again ;-)

Solution 3

I got the same message while having multiple forms on the same page. Turns out, symfony defaults to the name 'form' for all of them. Instead of using createFormBuilder, you can change the name of the form to avoid conflicts using

public FormBuilderInterface createNamedBuilder(string $name, string|FormTypeInterface $type = 'form', mixed $data = null, array $options = array(), FormBuilderInterface $parent = null)

See https://stackoverflow.com/a/13366086/1025437 for an example.

Share:
19,123
AnchovyLegend
Author by

AnchovyLegend

Updated on June 04, 2022

Comments

  • AnchovyLegend
    AnchovyLegend about 2 years

    I created a form using formBuilder in Symfony. I add some basic styling to the form inputs using an external stylesheet and referencing the tag id. The form renders correctly and processes information correctly.

    However, it outputs an unwanted unordered list with a list item containing the following text: This form should not contain extra fields.

    I am having a really hard time getting rid of this notice. I was wondering if someone can help me understand why it being rendered with my form and how to remove it?

    Many thanks in advance!

    Controller

    $form = $this->createFormBuilder($search)
            ->add('searchinput', 'text', array('label'=>false, 'required' =>false))
            ->add('search', 'submit')
            ->getForm();
    
    $form->handleRequest($request);
    

    Twig Output (form is outputted and processed correctly

    This form should not contain extra fields.
    

    Rendered HTML

    <form method="post" action="">
        <div id="form">
           <ul>
               <li>This form should not contain extra fields.</li>
           </ul>
           <div>
              <input type="text" id="form_searchinput" name="form[searchinput]" />
           </div>
           <div>
              <button type="submit" id="form_search" name="form[search]">Search</button>
           </div>
           <input type="hidden" id="form__token" name="form[_token]" value="bb342d7ef928e984713d8cf3eda9a63440f973f2" />
        </div>
     </form>
    
  • AnchovyLegend
    AnchovyLegend over 10 years
    Thanks for the reply, I'll give it a try. Why is the token field generated in the first place and why is it a problem?
  • nni6
    nni6 over 10 years
    I don't know what field is extra in your case, you can find this out very simply. Extra fields mean that in request there are some fields that there are not in form builder. And so form binding makes this error. May be by default token field is not appended in form builder, so you can specify that explicitly by adding options to form builder as I wrote you in the answer.
  • Acyra
    Acyra over 10 years
    The token field is generated as CSRF protection (see symfony.com/doc/current/book/forms.html#csrf-protection) This is built in to Sf2 and is almost certainly not the cause of the error. More likely you've hardcoded some hidden field into your form template and that field isn't part of the object you are binding to.
  • userfuser
    userfuser almost 10 years
    My case exactly.. After changing the builder, I kept doing the AJAX submit without refreshing the whole screen. Tnx, +1.
  • ziiweb
    ziiweb about 8 years
    There is a method called getExtraData() at symfony's FormInterface that could help.