Dynamic changing form with rails 6

Hello,

I’ld like to have a form, which can change with the input.

A short example:
A reservation is to be created. To do this, the user fills out the business event (for example, using select). Now the selected event must be sent to the server, so that the server determines the possible event-categories and offers them for further selection. Now the category is selected, which determines the maximum bookable tickets and the price per ticket. Finally, the number of tickets is selected and the price is calculated.
With Save the form is sent to the server.

I use Rails 6 with stimulus for simple dynamic form modifications (e.g. add or delete embedded fields is working meanwhile).

Now I’ld like to call the controller#new or controller#edit method again and again with the updated fields so that the whole form is server generated and refreshed. I prefer to compose the form server-side to have a single point of logic and/or validation. Stimulus should be only a smart frontend helper, transmitting the meanwhile filled fields to server and refreshing the form. It should act like a wizard, but without multipage…

Is there an example for this? I currently can’t get it to work, because I am a newbie.
Or is there a better way to do something like this?

Thank you for your help

Best regards
Markus

Markus,

I know it’s been awhile since you asked, but did you still have questions on how to approach this? I’m happy to advise if I can.

Yes, I still try to find the “best” solution for wizard based / input depended / dynamic forms.

Hey Markus,

If I were you, I’d render the entire form on the server and just style=display: none; the sections that don’t need to be shown until after a user has chosen a particular combination or specific input.

And you can display/hide the different pieces of the form using the data-action=change->Controller#desiredMethod that Stimulus has to show/hide the different sections or perform a Rails.ajax request back to the server for server-side validations and get any updates you need in real-time.

I basically just did the same thing for a CRM system, the contact details were all just forms being validated in real-time by Stimulus so people couldn’t create multiple leads with the same emails, or to alert the user of an invalid phone number. Good luck!

1 Like