How do people handle multi page forms in phoenix?

Hi all,

I’m currently implementing an onboarding process for new users which allows them to add data over multiple pages, which is only saved at the end. i.e. it’s equivalent to a multiple step/page form. Here are the characteristics of the solution I’m looking for:

  • nothing saved to the db until the last form page is submitted, and then everything is saved (I don’t want to handle temp/incomplete state)
  • separate pages for each step in the form (not hiding/showing with javascript as I’m keen to minimise client side JS)
  • use of standard validation from existing changesets i.e. data needs to be sent and returned from the server for each step completed

Is there a recommended way of doing this in phoenix? I can’t find any examples.

The best option I can think of right now is just using separate non-standard actions for each step. e.g.

:new_step_1, :create_step_1, :new_step_2, :create_step_2… :create

Does that make sense? :stuck_out_tongue:

Thanks!

1 Like

You can use the same route but use query parameters for the page information, or encode it into the session (in addition to encoding the current answers into the session). :slight_smile:

Perhaps you can do like @OvermindDL1 said with the params for each step (/register?step=1 and so on).

Regarding the data, maybe you can save that into a GenServer? So, when you finish step 1, start a GenServer that holds the state and keep adding to it. You can easily add a timeout to check after X amount of time to see if the state has changed recently and kill it if it’s stale.

Thanks @OvermindDL1 and @Linuus. Query params seems like the best way to go!

(The GenServer approach sounds interesting but I don’t have any experience with that and want to KISS.)