8.1.10 Handling Duplicate Form Submissions 3.1.4

See the light - agile, industrial strength, rapid web application development made easy

(Quick Reference) # 8.1.10 Handling Duplicate Form Submissions - Reference Documentation Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith, Lari Hotari Version: 3.1.4 ## 8.1.10 Handling Duplicate Form Submissions Grails has built-in support for handling duplicate form submissions using the "Synchronizer Token Pattern". To get started you define a token on the form tag:
    <g:form useToken="true" ...>
Then in your controller code you can use the withForm method to handle valid and invalid requests:
    withForm {
       // good request
    }.invalidToken {
       // bad request
    }
If you only provide the withForm method and not the chained `invalidToken` method then by default Grails will store the invalid token in a `flash.invalidToken` variable and redirect the request back to the original page. This can then be checked in the view:
    <g:if test="${flash.invalidToken}">
      Don't click the button twice!
    </g:if>
The withForm tag makes use of the session and hence requires session affinity or clustered sessions if used in a cluster. | Quick Reference (hide)