Symfony 2 This form must not contain additional fields

I created a form using formBuilder in Symfony. I am adding a basic style to the form inputs using an external stylesheet and referencing the tag id. The form correctly displays and processes information correctly.

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

I find it very difficult to get rid of this notice. I was wondering if anyone can help me understand why this is being done with my form and how to remove it?

Thank you very much 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 output 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> 
+7
forms symfony symfony1 formbuilder
source share
5 answers

It seems to me that you have a problem due to the marker field. If so, try adding createFormBuilder () parameters:

 $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 an additional field, use this code in the controller, where you will receive a 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); 
+9
source share

This message is also possible if you added / changed fields in createFormBuilder () and click "Refresh" in your browser ...

In this case, this is normal after the form is submitted again; -)

+4
source share

I received the same message, having multiple forms on the same page. It turns out that symfony uses the name 'form' by default 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>

+1
source share

I encountered this error while creating a multi-stage form.

When submitting a form in step 1, the $ request-> request contains an acme_mybundle_myform array. This created a validation error and stopped the correct filling of the back, forward and form fields. Not to mention "this-form-should-not-contains-extra-fields"

I discovered this thanks to nni6 code.

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
source share

I had the same error.

This is because I had a form that, by mistake, had the name NULL.

In HTML, the name attribute will look like this:

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

The easiest way.

This may not be true for everyone, but worth checking out.

0
source share

All Articles