0

I am currently writing an application that will validate form data from an admin.

As far as I can see I have two option to do this:

GET and POST requests to the same URL

GET /admin/category/new present an HTML form to create a new category

POST /admin/category/new which POSTs here, if valid it just reloads the HTML form along with the submitted data and relevant errors until it passes validation. However if a user refreshes they are asked by the browser to resend the data.

GET and POST to different RESTful URLs

GET /admin/category/new present an HTML form to create a new category

POST /admin/category which would could handles PUT, DELETE requests as well. If the validation fails and a user is redirected back to GET /admin/category/new is it OK to persist both the error and previous input in session flash? What would happen if a user was submitting multiple edits across a number of browser windows. How do you ensure that previous input is attached to the right form.

The main reason I ask is that when I am updating multiple items in Magento or WordPress multiple flash messages queue in one window that relate to other windows updates. So which option is the best? or easiest to maintain?

  • possible duplicate of [Reload browser window after POST without prompting user to resend POST data](http://stackoverflow.com/questions/4869721/reload-browser-window-after-post-without-prompting-user-to-resend-post-data) – Gajus Apr 10 '14 at 05:53

1 Answers1

0

I suggest using the POST-redirect-GET design pattern.

POST-Redirect-GET, or GET-After-POST, is a design pattern used by some web applications in which the web browser is redirected to a page immediately after the web server finishes processing an HTTP POST request. This can improve usability after a user submits a form by letting the user refresh the page or browse back to the page without resubmitting the form.

Personally I find this leads to cleaner code and is easier to maintain.

Matt S
  • 14,976
  • 6
  • 57
  • 76
  • OK, but where to persist data that has failed validation to repopulate form? Is using session enough or can multiple requests make cause data from request n load in request n+1 for example? – user2856585 Oct 13 '13 at 20:08
  • I prefer to only redirect after form processing succeeds. No session necessary. That way I can easily refill the form with the posted unsaved data. – Matt S Oct 13 '13 at 21:20
  • Ah, so you use the first option and `POST` to the same URL, rather than the second pattern that mirrors Ruby on Rails routing. – user2856585 Oct 14 '13 at 09:18
  • I see from this question: http://stackoverflow.com/questions/7843271/rails-confused-by-url-change-when-model-validations-fail why Option 1 is the best method. Thank you! – user2856585 Oct 14 '13 at 09:21