Error
The CSRF token is invalid. Please try to resubmit the form.
This error appearing sometimes in symfony2 forms , but after refreshing form saved succesfully.
This error is not frequrent it happening sometimes while saving form.
I have tried in every browser issue is same.
On Refresh of Page Token get refresh in hidden input fields But error remains the same.
i have googled and found some helpfull article which suggest
{{form_rest(form)}}
to use before closing of form tag.
Here is my code
Twig code
<div id="{{ formID }}_container" class="main">
<div class="page">
<div id="{{ formID }}_output"></div>
{% include 'Bundle:Form:required_msg.html.twig' %}
{{form_start(form, { 'attr' : { 'id': formID } })}}
<div style="display: none">
{{form_rest(form)}}
</div>
{{form_end(form)}}
</div>
</div>
I have this hidden field:
<input id="form_id" name="details_form[_token]" value="somevalue" type="hidden">
This is my form data:
details_form[_token] value
details_form[age]
details_form[gender] F
This is the request response:
{"success":false,"msg":"Form is invalid.","errors":[{"name":"[0]","message":"The CSRF token is invalid. Please try to resubmit the form."}]
Some other answer I've seen on Stack Overflow (The CSRF token is invalid. Please try to resubmit the form) says to do this:
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'options' => array(),
csrf_protection' => false
));
}
But, my mequirement is not to set the csrf_protection
flag to false
. Can anyone suggest the best to way to handle this issue?