In an effort to have everything translateable in our website ( including the error messages for the validations ), we switched almost all of our forms to remote forms. While this helps with the ability to translate error messages, we have encountered other problems, like:
- if the user clicks on the submit button multiple times, the action gets called multiple times. If we have a remote form for creating a new record in the database, and assuming that the user’s data is valid, each click will add a new object ( with the exact same contents ). Is there any way of making sure that such things cannot happen?
Is there somewhere I could read about remote forms best practices? How could I handle the multiple clicks problem? Is switching all the forms to remote forms a very big mistake?
The simplest solution would be to generate a token for each form. Then your
createaction could make sure it hasn’t been used yet and determine whether the record should be created.Here’s how I would go about writing this feature. Note that I haven’t actually tested this, but the concept should work.
1.
Inside the
newaction create a hash to identify the form request.2.
Add a hidden field to the form that stores the form token. This will be captured in the
createaction to make sure the form hasn’t been submitted before.3.
In the
createaction you can make sure the form token matches between thesessionandparamsvariables. This will give you a chance to see if this is the first or second submission.Update: What I have described above has a name, it is called a Synchronizer (or Déjà vu) Token. As described in this article, is a proper method to prevent a double submit.