1

I have a form wich is generated with a random algo (quizz test) . after submit the render is a result page : the problem is if the user click 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 dont display the submit button etc... but even when the variable is removed in my controller, twig still consider it as defined ... ?? do u have another simple solution ?

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
  • 6,956
  • 1
  • 9
  • 26