Symfony 2 This form should not contain extra fields

AnchovyLegend picture AnchovyLegend · Oct 6, 2013 · Viewed 18.7k times · Source

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>

Answer

nni6 picture nni6 · Oct 6, 2013

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);