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

后端 未结 15 1443
粉色の甜心
粉色の甜心 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:58

    Also you can see this error message when your form has a lot of elements.

    This option in php.ini cause of problem

    ; How many GET/POST/COOKIE input variables may be accepted
     max_input_vars = 1000
    

    Problem is that _token field misses PUT (GET) request, so you have to increase value.

    Also, it concerns a big files. Increasing the

    upload_max_filesize
    

    option will solve problem.

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

    Before your </form> tag put:

    {{ form_rest(form) }}
    

    It will automatically insert other important (hidden) inputs.

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

    In addition to others' suggestions you can get CSRF token errors if your session storage is not working.

    In a recent case a colleague of mine changed 'session_prefix' to a value that had a space in it.

    session_prefix: 'My Website'
    

    This broke session storage, which in turn meant my form could not obtain the CSRF token from the session.

    0 讨论(0)
  • 2020-11-28 11:02

    You need to add the _token in your form i.e

    {{ form_row(form._token) }}
    

    As of now your form is missing the CSRF token field. If you use the twig form functions to render your form like form(form) this will automatically render the CSRF token field for you, but your code shows you are rendering your form with raw HTML like <form></form>, so you have to manually render the field.

    Or, simply add {{ form_rest(form) }} before the closing tag of the form.

    According to docs

    This renders all fields that have not yet been rendered for the given form. It's a good idea to always have this somewhere inside your form as it'll render hidden fields for you and make any fields you forgot to render more obvious (since it'll render the field for you).

    form_rest(view, variables)

    0 讨论(0)
  • 2020-11-28 11:03

    This seems to be an issue when using bootstrap unless you are rendering the form by {{ form(form)}}. In addition, the issues seems to only occur on input type="hidden". If you inspect the page the with the form, you'll find that the hidden input is not part of the markup at all or it's being rendered but not submitted for some reason. As suggested above, adding {{form_rest(form)}} or wrapping the input like below should do the trick.

    <div class="form-group">
        <input type="hidden" name="_csrf_token" value="{{ csrf_token('authenticate') }}">
    </div>
    
    0 讨论(0)
  • 2020-11-28 11:10

    If you have converted your form from plain HTML to twig, be sure you didn't miss deleting a closing </form> tag. Silly mistake, but as I discovered it's a possible cause for this problem.

    When I got this error, I couldn't figure it out at first. I'm using form_start() and form_end() to generate the form, so I shouldn't have to explicitly add the token with form_row(form._token), or use form_rest() to get it. It should have already been added automatically by form_end().

    The problem was, the view I was working with was one that I had converted from plain HTML to twig, and I had missed deleting the closing </form> tag, so instead of :

    {{ form_end(form) }}
    

    I had:

    </form>
    {{ form_end(form) }}
    

    That actually seems like something that might throw an error, but apparently it doesn't, so when form_end() outputs form_rest(), the form is already closed. The actual generated page source of the form was like this:

    <form>
        <!-- all my form fields... -->
    </form>
    <input type="hidden" id="item__token" name="item[_token]" value="SQAOs1xIAL8REI0evGMjOsatLbo6uDzqBjVFfyD0PE4" />
    </form>
    

    Obviously the solution is to delete the extra closing tag and maybe drink some more coffee.

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