T
TanStack2y ago
optimistic-gold

Add/Delete fields in a form based on another field

Hi there! I made a form with TanStack-Form / React (see video) where I manage the display of two extra fields if I select the "Good Til Time" option in my select list. For the moment, the fields are flat and not nested, I just hide them and ignore their values in the submit, but it's not ideal. I haven't found any examples of how to add nested fields properly (I see there's the possibility of manipulating indexes). But if anyone has managed to do an example, I'd love to 🙏🏽 . Thanks a lot and have a nice day guys. ps: the lib experience so far is great
3 Replies
ambitious-aqua
ambitious-aqua2y ago
Ahh, so you're talking about dynamic fields based on other fields? Like "show A when B is set to this value"?
quickest-silver
quickest-silver2y ago
I think you'll just need to have some optional fields in your form, and display them with something like {form.getFieldValue('timeInForce') === 'Good til time' && <OtherInputs />} Then to be safe, in the form validation, you can make sure that those fields are provided when timeInForce is correct
optimistic-gold
optimistic-goldOP2y ago
Yes exactly Ok thanks, I'll give it a try 👌

Did you find this page helpful?