Invalid symfony2 form without errors

I have a problem with the Symfony2 CRUD form created. (With MongoDB docs, but I don't think this is related)

In my createAction () method of my controller, when I debug the form result:

$form->isValid() // returns false $form->getErrors() // returns en empty array(0) {} 

So, I am not getting anything using form_errors(form) in my twig template (which seems normal due to $form->getErrors() empty return)

And the recorded values ​​are not replaced in the form ...

Does anyone have any ideas?

+49
validation forms symfony
Jun 26 2018-12-12T00:
source share
11 answers

The first thing to understand is a check on the model, not on the form. A form may contain errors, but only if it has a field associated with a property that is not validated. Therefore, if your form does not contain an invalid field (perhaps a NotNull statement for a property that is not on the form), it will not show an error.

Secondly, $form->getErrors() will only show errors for this level, each of which may contain its own errors. Therefore, if you want to check for errors, you must go through the fields and call getErrors in each field. The getErrors method in the Form class can trick this path.

+49
Jun 27 2018-12-12T00:
source share

To debug a form, use $form->getErrorsAsString() instead of $form->getErrors() .

$form->getErrorsAsString() should only be used to debug the form ... it will contain errors for each child, which does not apply to $form->getErrors() .

As Peter mentions, $form->getErrors() will not return the sum of all errors of child forms.

To understand how a form can be invalid and have getErrors () returning an empty array, you can take a look at the isValid () method's symfony form class. As you can see, there are two cases where the form is invalid, the first test for the general form and the second random test for each child.

 public function isValid() { //... //CASE I : IF CHILD ELEMENTS HAVE ERRORS, $this->errors WILL CONTAIN //THE ERROR ON THE CHILD ELEMENT AND NOT ON THE GENERAL 'errors' FIELD //ITSELF if (count($this->errors) > 0) { return false; } //CASE II: AND THIS IS WHY WE ARE TESTING THE CHILD ELEMENTS AS WELL //TO CHECK WHETHER THERE ARE VALID OR NOT if (!$this->isDisabled()) { foreach ($this->children as $child) { if (!$child->isValid()) { return false; } } } return true; } 

Therefore, each child of the form may contain an error, but $form->getErrors() itself will not return all errors. Given a form that has many children, you usually have $ form-> getErrors () with a CSRF error if the CSRF is incorrect.

+56
Dec 29
source share

Update for Symfony 2.6

So, depending on you version of Symfony2:

 die($form->getErrorsAsString()); 

According to , <Function t21> is deprecated (will be removed in Symfony3), and you should use the following method:

 die((string) $form->getErrors()); // Main errors die((string) $form->getErrors(true)); // Main and child errors 

Like , you can also use the dump (dev) function if you activated DebugBundle :

 dump((string) $form->getErrors()); // Main errors dump((string) $form->getErrors(true)); // Main and child errors 
+43
Oct 22 '14 at 9:30 a.m.
source share

I only have the same problem. For me, the form is not valid, but I could not get any errors using $form->getErrors() or $form->getErrorsAsString() . Later, I discovered that I forgot to pass the CSRF token to the form so that it would not be submitted, and $form->handleRequest($request) did nothing (without checking). When I saw @pit answer, I tried using

 $form->submit($request); $form->getErrorsAsString(); 

he returned an error:

ERROR: CSRF token is not valid. Retry submitting the form.

Here are a few clarifications in the Symfony2 documentation: http://symfony.com/doc/current/book/forms.html#handling-form-submissions

+16
Dec 21 '13 at 11:06
source share

For me, the form was not submitted, even if I had a submit button. I added code to solve the problem

 $request = $this->get('request'); if($request->isMethod("POST")){ $form->submit($request); if($form->isValid()){ // now true } } 
+3
Sep 11 '13 at 20:02
source share

If you submit data via AJAX , you may have missed the inclusion of the form name in your data keys and therefore are a “victim” ...

 # line 100 of Symfony/Component/Form/Extension/HttpFoundation/HttpFoundationRequestHandler.php // Don't submit the form if it is not present in the request 

This means that when you try to process the request, the request processing mechanism did not find your form name inside the GET / POST data (which means as an array).

When visualizing a form in the usual way, each of its fields contains your form name as a prefix in its name attribute my_form[child_field_name] .

When using ajax, add your form name as a prefix to the data!

 data : { "my_form" : { "field_one" : "field_one_value" ... } } 
+2
Jun 28 '16 at 15:07
source share

Starting with Symfony 3, according to the documentation, you should use the new implementation:

$errors = (string) $form->getErrors(true, false);

This returns all errors as a single line.

+2
Jan 25 '17 at 11:19 on
source share

Yes it is correct that he says Peter Kruithof In SF 2.8 it is my function to get field errors

  private function getErrorsForm(\Symfony\Component\Form\Form $form) { $response = array(); foreach ($form as $child) { foreach ($child->getErrors(true) as $error) { $response[$child->getName()][] = $error->getMessage(); } } return $response; } 
+1
Nov 30 '16 at 15:36
source share

I ran into this error and found that I forgot to "process" the request. Make sure you have it ...

 public function editAction(Request $request) { $form = $this->createForm(new CustomType(),$dataObject); /** This next line is the one I'm talking about... */ $form->handleRequest($request); if ($request->getMethod() == "POST") { if ($form->isValid()) { ... 
+1
Dec 14 '16 at 5:30
source share

It looks like you have a validation problem. The form is not confirmed upon submission. I am going to suggest that you use annotations for your verification. Make sure you have it at the top of the object.

 use Symfony\Component\Validator\Constraints as Assert; 

and also it is above each property

 /** * @Assert\NotBlank() */ 

NotBlank() can be changed to any restriction that suits your needs.

More information about verification can be found at: http://symfony.com/doc/current/book/validation.html

Additional information about Assert restrictions can be found at: http://symfony.com/doc/current/book/validation.html#constraints

0
Jun 27 '12 at 18:19
source share

For Symfony (> = 3.2 - 4) you can use:

 foreach($form->getErrors(true, false) as $er) { print_r($er->__toString()); } 

to see errors.

0
Oct 18 '17 at 9:35 on
source share



All Articles