Symfony2 invalid form without errors

前端 未结 11 733
孤独总比滥情好
孤独总比滥情好 2020-12-01 01:46

I\'ve got a problem with a Symfony2 generated CRUD form. (With MongoDB Documents, but I do not think that this is related)

In my controller\'s createAction() method,

相关标签:
11条回答
  • 2020-12-01 02:15

    If you are sending datas via AJAX, you may have missed to include the form's name on your datas keys and therefore are "victim" of …

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

    Which means, while trying to handle the request, the request processing mechanism did not find your form's name inside GET/POST datas (meaning as an array).

    When you render a form the usual way, each of its fields contain your form's name as a prefix into their name attribute my_form[child_field_name].

    When using ajax, add your form's name as a prefix in datas !

    data : {
        "my_form" : {
           "field_one" : "field_one_value"
           ...
        }
    }
    
    0 讨论(0)
  • 2020-12-01 02:16

    I’ve just got the same problem. For me, the form was not valid, but I could not get any errors by using $form->getErrors() or $form->getErrorsAsString(). I later found I forgot to pass the CSRF token to the form so it won’t be submitted, and $form->handleRequest($request) did nothing (no validation). As I saw @pit's answer, I tried to use

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

    it returned an error:

    ERROR: The CSRF token is invalid. Please try to resubmit the form.

    Here is some explanation in the documentation of Symfony2: http://symfony.com/doc/current/book/forms.html#handling-form-submissions

    0 讨论(0)
  • 2020-12-01 02:16

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

    $request = $this->get('request');
    if($request->isMethod("POST")){
          $form->submit($request);
            if($form->isValid()){
            // now true
            }
    }
    
    0 讨论(0)
  • 2020-12-01 02:17

    Yes it is correct, what it say Peter Kruithof In SF 2.8 this is my function,to get the errors of the fields

     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;
    }
    
    0 讨论(0)
  • 2020-12-01 02:18

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

    $form->getErrorsAsString() should only be used to debug the form...it will contain the errors of each child elements which is not the case of $form->getErrors().

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

    To understand how a form can be invalid and have a getErrors() returning an empty array, you can have a look at the isValid() method of the symfony form class. As you can see, there are 2 cases where the form is not valid, the first one test for the general form, and the second case test for each child elements.

    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 form child can contain an error, but $form->getErrors() itself won't return all the errors. Considering a form that has many child elements, you will generally have $form->getErrors() with a CSRF error if the CSRF is not correct.

    0 讨论(0)
  • 2020-12-01 02:26

    The first thing to understand is validation is done on the model, not the form. The form can contain errors, but only if it has a field mapped to the property that doesn't validate. So if your form does not contain the invalid field (maybe a NotNull assertion on a property that is not in the form), it will not show the error.

    The second thing is that $form->getErrors() will only show errors for that level, each form child can contain its own errors. So if you want to check the errors, you should loop through the fields and call getErrors on each field. The getErrors method on the Form class can be deceiving that way.

    0 讨论(0)
提交回复
热议问题