Temak
Temak

Reputation: 3009

Multi-step flows in Play

I am trying to implement some flow control in Play 2.0.3, kind of a wizard with several steps. What is the best way to do it in Play?

I want to have multi-step flows, like step1 -> step2 -> finish

The main problem is not to allow user to skip for example step2 and go straight to finish (user can manually input a link in the browser /finish ).

Thanks. Please let me know if I haven't explained myself well.

Upvotes: 2

Views: 1069

Answers (3)

adis
adis

Reputation: 5951

I would store the steps in the database (for long term) or in the cache (on short term). This way you can build some logic if (secondQuestionAnswered) { .. } and be sure that your user did not skipped anything and / or the user can finish the steps when he wants to. And persisting in DB or cache would also benefit if your app grow and you need to scale it.

Upvotes: 1

Julien Richard-Foy
Julien Richard-Foy

Reputation: 9663

A completely stateless way could consist in storing all data in the URLs of steps, where the step n + 1 contains all data from step n:

GET   /step1    controllers.Application.step1
GET   /step2    controllers.Application.step2(dataFromStep1)
GET   /step3    controllers.Application.step3(dataFromStep1, dataFromStep2)
POST  /finish   controllers.Application.finish

The advantages of this solution are its simplicity and the fact that checking that all required steps have been completed before the next step is performed for free by the Play! router.

However, if you have a lot of data your URLs will be huge, making this solution less applicable. In such a case, you need to:

  • save data of each step to a persistent storage (using the session is yet another option, but limited in size to 4KB) ;
  • identify your users so e.g. the /step2 URL won’t return the same thing for different users ;
  • set a timeout to discard an incomplete submission and clean your persistent storage ;
  • manually check in your business code that all steps have been completed for the final submission.

Upvotes: 2

biesior
biesior

Reputation: 55798

You have at least two options:

  • Use POST method for sending data between steps, that will be using always the same URL and if user will refresh the page he'll back to first step. Your step identifier will be just hidden form field.
  • Use jQuery for displaying next steps, you don't need to even send data between steps to the server. You can build one big form (if it's form) and show/hide its parts with jQuery. on the finish you'll perform common form's submit with all steps inside and there you can validate and show 'thank you' page.
  • Optionally you can use AJAX with jQuery to separate validating and saving steps.

Upvotes: 4

Related Questions