Need advice on how to do validations in with hotwire/stimulus

Rails 7 app, and I have a form that primarily drives off of an employee ID number being entered. What I would like to do is disable all fields initially except the ID field and when the id number is entered, have rails query the DB to verify if it is active. If so enable the other form fields on the page to be used (selects, checkboxes, date selects etc.).

Not sure how to even go about this so was looking for advice or examples if there is anything like this.

Thanks!

The easiest way to think about this is as two forms. The first is not unlike a search form, using GET and then loading the second form, which will be set to POST or PATCH, depending on whether the intent is to add a new record or update an existing one.

To begin with, try modeling this as a normal full form submit both times, no stimulus or other JS involved. Make your normal Rails validations as you would for each of those forms. Then you can layer the JS on top later if you want the UX to be more wizzy.

You can probably get almost everything you want using turbo-frames, and that won’t require any fancy coding or other hand-written JS. But start with a normal form, just like it’s 1997 or something.

Walter

1 Like

Thank you for the info! This sounds like a great approach to get started. I will get working on this.

1 Like