Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 2 This form should not contain extra fields

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>
like image 846
AnchovyLegend Avatar asked Oct 06 '13 14:10

AnchovyLegend


2 Answers

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);
like image 130
nni6 Avatar answered Oct 04 '22 20:10

nni6


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

like image 28
PBR Avatar answered Oct 04 '22 21:10

PBR