Help with building forms with dynamic fields
Hi, I'm trying to use tanstack-form to help build a form that has dynamic elements but struggling.
I have an API that returns data for the fields that looks like:
and later in the component
9 Replies
conscious-sapphireOP•5mo ago
Issues i'm having:
1. I'm dynamically building the
defaultValues in useForm. Is this the right way to do this? I think not because of point 2
2. I noticed on submit, the data sent contains the dynamic data but the type thinks summary is the only thing there.
3. Where I render the field and set the name, I see Type 'string' is not assignable to type '"summary"'. which means the dynamic fields are not working the way I want them to.
4. I noticed the value of the field only renders for names that have no dots. e.g threshold value in the input field renders fine but transfer.fee does not.
I'll appreciate any help on how to build a form like this 🥺conscious-sapphireOP•5mo ago
Here's the full component
exotic-emerald•5mo ago
this is your problem

exotic-emerald•5mo ago
TypeScript fails to see that you have a string index
try extracting defaultValues like so:
conscious-sapphireOP•5mo ago
Thanks @Luca | LeCarbonator , that did the trick for the types. How do i solve problem 4 ?
value={field.state.value} <- this breaks when the key is something with dot notationexotic-emerald•5mo ago
well, the name
transfer.fee implies it's of structure
TypeScript doesn't really let you have strings with reserved characters, so it won't warn you. I also don't think it's written in the documentation, merely implied
try to avoid the characters ., [ and ]
also if you know a place in the docs where this ought to be mentioned, let me knowconscious-sapphireOP•5mo ago
I see, I should probably switch to kebab case for all the keys then
Much appreciated Luca, saved me a lot of headache here!
I have other questions but will create a separate post when i get there 🙂
stormy-gold•3mo ago
@Emperor Can you provide a working version for reference? I'm looking to do something similar with variable length/type attributes from the server
conscious-sapphireOP•3mo ago
I dont have one unfortunately, Is there anything above that's not clear and I can explain how i addressed it?