1

I have a form which is generated with a random algorithm (quiz test). After submit the render is a result page: the problem is if the user clicks on the back arrow, the form with the same questions will be displayed and can be submitted again! I tried to set a session variable and play with twig function { if app.sesseion->get('myVaryable') is defined } then don't display the submit button etc... but even when the variable is removed in my controller, twig still consider it as defined? Do you have another simple solution?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
moko
  • 15
  • 3

1 Answers1

2

Clicking the back button in the browser usually results in a page reloaded from the cache (no interaction with the server) so you can't control this with twig (back-end code) unless the user refreshes the page (re-sends request to server).

You need a javascript (front-end code) solution like below, to check the navigation type.

if (window.performance && window.performance.navigation.type == window.performance.navigation.TYPE_BACK_FORWARD) {

    // Disable the form
    var inputs = document.getElementsByTagName("INPUT");
    for (var i = 0; i < inputs.length; i++) {
        inputs[i].disabled = true;
        if (inputs[i].type === 'submit') {
            inputs[i].remove();   
        }
    }

}

I recommend you also look for, and appropriately handle, duplicate submissions in the controller that handles the form, as any skilled user could still submit the form.

References:

detecting back button click

disabling inputs

Arleigh Hix
  • 9,990
  • 1
  • 14
  • 31