Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

twig

symfony

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?

like image 751
Francisco Albert Avatar asked May 04 '14 11:05

Francisco Albert


People also ask

What does the CSRF token is invalid Please try to resubmit the form?

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.

What is the CSRF token?

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.


2 Answers

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)

like image 153
M Khalid Junaid Avatar answered Oct 05 '22 10:10

M Khalid Junaid


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.

like image 31
zalex Avatar answered Oct 05 '22 10:10

zalex