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
My form code is this:
<form novalidate action="{{path('signup_index')}}" method="post" {{form_enctype(form)}} role="form" class="form-horizontal"> <div class="form-group"> {{ form_label(form.email, 'Email', {'label_attr': {'class': 'col-md-1 control-label'}}) }} {{ form_widget(form.email, {'attr': {'class': 'col-md-2'}}) }} {{ form_errors(form.email) }} </div> <div class="form-group"> {{ form_label(form.nickname, 'Nickname', {'label_attr': {'class': 'col-md-1 control-label'}}) }} {{ form_widget(form.nickname, {'attr':{'class': 'col-md-2'}}) }} {{ form_errors(form.nickname, {'attr': {'class': 'col-md-3'}}) }} </div> <div class="form-group"> {{ form_label(form.password, 'password', {'label_attr': {'class': 'col-md-1 control-label'}}) }} {{ form_widget(form.password, {'attr': {'class': 'col-md-2'}}) }} {{ form_errors(form.password, {'attr': {'class': 'col-md-3'}}) }} </div> <div class="form-group"> {{ form_label(form.password_repeat, 'Repeat password', {'label_attr': {'class': 'col-md-1 control-label'}}) }} {{ form_widget(form.password_repeat, {'attr':{'class': 'col-md-2'}}) }} {{ form_errors(form.password_repeat, {'attr': {'class': 'col-md-3'}}) }} </div> <div class="form-group"> <div class="col-md-1 control-label"> <input type="submit" value="submit"> </div> </div> </form>
Any ideas?
This error message means that your browser couldn't create a secure cookie, or couldn't access that cookie to authorize your login. This can be caused by ad- or script-blocking plugins, but also by the browser itself if it's not allowed to set cookies.
A CSRF token is a secure random token (e.g., synchronizer token or challenge token) that is used to prevent CSRF attacks. The token needs to be unique per user session and should be of large random value to make it difficult to guess. A CSRF secure application assigns a unique CSRF token for every user session.
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With