8

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
  • 12,139
  • 38
  • 147
  • 231
  • 1
    possible duplicate of [Symfony2: get rid of "This form should not contain extra fields"](http://stackoverflow.com/questions/8475999/symfony2-get-rid-of-this-form-should-not-contain-extra-fields) – falinsky Oct 06 '13 at 16:00

5 Answers5

9

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);
Jake N
  • 10,535
  • 11
  • 66
  • 112
nni6
  • 990
  • 6
  • 13
  • 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? – AnchovyLegend Oct 06 '13 at 16:53
  • 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. – nni6 Oct 07 '13 at 09:57
  • The token field is generated as CSRF protection (see http://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. – Acyra Jan 22 '14 at 11:44
  • There is a method called `getExtraData()` at symfony's `FormInterface` that could help. – tirenweb May 30 '16 at 12:02
4

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

PBR
  • 306
  • 4
  • 15
  • 1
    My case exactly.. After changing the builder, I kept doing the AJAX submit without refreshing the whole screen. Tnx, +1. – userfuser Jul 23 '14 at 13:47
1

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.

Community
  • 1
  • 1
Mondane
  • 498
  • 1
  • 5
  • 21
0

I ran into this error when creating a multi-step form.

When the step 1 form is submitted, $request->request contains acme_mybundle_myform array. This created a validation error and stopped the back, forward and form fields from populating correctly. Not to mention "this-form-should-not-contain-extra-fields"

I discovered this thanks to the code by nni6.

The solution in my case was inside the controller:

if ($form->isValid())
{
    if($form->has('nextStep') && $form->get('nextStep')->isClicked())
    {
        $session->getFlashBag()->set('notice', 'Next clicked');

        $registerType->incrementStep();

        $request->request->remove('acme_mybundle_myform');

        return $this->forward("AcmeMyBundle:Default:register", array($request));

    }
....
}
0

I had the same error.

It was because I had a form which, by mistake, had a NULL name.

In the HTML, the name attribute would look like this:

<form name href="..." action"..."></form>

As simple as that.

Might not be the case for everyone, but worth to check.

Tortus
  • 141
  • 7