2

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?

Tomsgu
  • 1,016
  • 1
  • 11
  • 31
afeef
  • 4,396
  • 11
  • 35
  • 65
  • Maybe you can try this https://stackoverflow.com/a/21366066/9750031 – Andrew Vakhniuk Jun 05 '18 at 10:50
  • Nope nothing work for me any other solution please – afeef Jun 05 '18 at 12:21
  • Maybe you can do like that `csrf_protection' => false` and generate the token and check it manually by yourself, maybe in the event listener – Andrew Vakhniuk Jun 05 '18 at 13:15
  • can you tell why csrf token occurs even if other forms are working – afeef Jun 05 '18 at 13:33
  • Did you check wheher token input hidden is present right before csrf error occurs? – Andrew Vakhniuk Jun 05 '18 at 13:43
  • 1
    And whether your form big or not? if it is big one check this https://stackoverflow.com/a/26766676/9750031 – Andrew Vakhniuk Jun 05 '18 at 13:45
  • Could it be be because the token is inside _display: none_ ? can you remove that css and try again? – gvf Jun 05 '18 at 21:30
  • Thanks andrew this solution worked for me. – afeef Jun 06 '18 at 06:49
  • can anyone suggest how can get count of max_input_vars with csrf token currently i have followed url https://stackoverflow.com/questions/12169818/in-php-how-can-i-detect-that-input-vars-were-truncated-due-to-max-input-vars-be – afeef Jun 06 '18 at 09:07
  • which is showing 1000 length ofmax_input_var but im not sure does it count csrf token as well please suggest – afeef Jun 06 '18 at 09:08
  • Possible duplicate of [The CSRF token is invalid. Please try to resubmit the form](https://stackoverflow.com/questions/23455780/the-csrf-token-is-invalid-please-try-to-resubmit-the-form) – M Khalid Junaid Jul 05 '18 at 06:39

0 Answers0