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

后端 未结 15 1485
粉色の甜心
粉色の甜心 2020-11-28 10:38

I\'m getting this error message every time I try to submit the form:

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

相关标签:
15条回答
  • 2020-11-28 10:47

    In case you don't want to use form_row or form_rest and just want to access value of the _token in your twig template. Use the following:

    <input type="hidden" name="form[_token]" value="{{ form._token.vars.value }}" />
    
    0 讨论(0)
  • 2020-11-28 10:48

    In my case I got a trouble with the maxSize annotation in the entity, so I increased it from 2048 to 20048.

     /**
     * @Assert\File(
     *     maxSize = "20048k",
     *     mimeTypes = {"application/pdf", "application/x-pdf"},
     *     mimeTypesMessage = "Please upload a valid PDF"
     * )
     */
    private $file;
    

    hope this answer helps!

    0 讨论(0)
  • 2020-11-28 10:49

    I had this error recently. Turns out that my cookie settings were incorrect in config.yml. Adding the cookie_path and cookie_domain settings to framework.session fixed it.

    0 讨论(0)
  • 2020-11-28 10:53

    This happens because forms by default contain CSRF protection, which is not necessary in some cases.

    You can disable this CSRF protection in your form class in getDefaultOptions method like this:

    // Other methods omitted
    
    public function getDefaultOptions(array $options)
    {
        return array(
            'csrf_protection' => false,
            // Rest of options omitted
        );
    }
    

    If you don't want to disable CSRF protection, then you need to render the CSRF protecion field in your form. It can be done by using {{ form_rest(form) }} in your view file, like this:

    <form novalidate action="{{path('signup_index')}}" method="post" {{form_enctype(form)}} role="form" class="form-horizontal">
        <!-- Code omitted -->
    
        <div class="form-group">
            <div class="col-md-1 control-label">
                <input type="submit" value="submit">
            </div>
    
        </div>
        {{ form_rest(form) }}
    </form>
    

    {{ form_rest(form) }} renders all fields which you haven't entered manually.

    0 讨论(0)
  • 2020-11-28 10:54

    I hade the same issue recently, and my case was something that's not mentioned here yet:

    The problem was I was testing it on localhost domain. I'm not sure why exactly was this an issue, but it started to work after I added a host name alias for localhost into /etc/hosts like this:

    127.0.0.1        foobar
    

    There's probably something wrong with the session while using Apache and localhost as a domain. If anyone can elaborate in the comments I'd be happy to edit this answer to include more details.

    0 讨论(0)
  • 2020-11-28 10:54

    I faced a similar issue. After ensuring the token field was actually rendered (see accepted answer) I checked my cookies. There were 2(!) cookies for the domain in my Chrome browser, apparently because I was running the application on the same domain as another app, but with a different port (i.e. mydomain.com set the original cookie while the buggy app was running on mydomain.com:123) Now apparently Chrome sent the wrong cookie so the CSRF protection was unable to link the token to the correct session.

    Fix: clear all the cookies for the domain in question, make sure you don't run multiple applications on the same domain with differing ports.

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