"Right" way to do conditional forms
Hello, first of all, thanks for all the work with Ash, it's being incredible and I'm very happy to using it. It's really awesome.
I'm facing a challenge here, and just have some questions if maybe there is a way that AshPhoenix forms can help me here.
I have a very complex form, where several inputs change based on the selection of some attributes.
For example, for this attribute:
I render this:
But then I have several inputs that render differently if is foo, bar or baz.
And also I actually have other attributes with different conditions too.
My question is, is there a way to do this without manually setting what shows up or not? And also cleaning the form when they change?
Ideally:
selected foo -> foo_field is filled and submitted, bar_field and baz_field are nil
selected foo -> foo_field is filled, but then I change to bar and bar_field is filled and submitted, then foo_field is nil
selected foo -> foo_field is filled, but then I change to bar and bar_field is filled and then returned to foo and submitted, then foo_field is present and bar_field is nil
I thought of using nested forms but multi conditionals not sure if it will work
Right now I could make it work to show or hide fields adding a phx-change to the select, but if I don't "clean" the form it will send data that was hidden
I appreciate any help on this matter.
Solution:Jump to solution
I think you'll essentially need to clean up the UI and/or model your input to the action as a union, and use the union form guides.
5 Replies
Its an interesting question, I'm not sure if I have a great answer for you really.
Solution
I think you'll essentially need to clean up the UI and/or model your input to the action as a union, and use the union form guides.
Hexdocs Search Results
Searched
ash_phoenix-2.3.6
for union forms
ash_phoenix-2.3.6 | extras
ash_phoenix-2.3.6 | extras
ash_phoenix-2.3.6 | extras
ash_phoenix-2.3.6 | extras
ash_phoenix-2.3.6 | extras
ash_phoenix-2.3.6 | extras
top result in that search ^
interesting, thanks for the reply @Zach Daniel, I was reading to do embedded data layer without even noticing the union form. I think that makes sense, I will try that, thanks!