T
TanStack2w ago
like-gold

withForm usage questions

I was trying to use withForm for the first time now that we are starting to get into some more complex forms and might go back and break up some of our others after messing around with it a bit. But I am running into what seems like a bug but I might be missing something. Wasn't able to find any information related to this though so unsure. https://stackblitz.com/edit/tanstack-form-6y8wrlas?file=src%2Findex.tsx Here in my demo if you uncomment the onSubmitAsync I start to get some typing issues on the FirstNameField props. Is this a bug? Or if expected how can I fix this here.
Tim Hazlett
StackBlitz
Form Standard Schema Example (duplicated) - StackBlitz
Run official live example code for Form Standard Schema, created by Tanstack on StackBlitz
5 Replies
ugly-tan
ugly-tan2w ago
withForm is strict and expects the same validators and values to be present in the form that's passed to it. If you want to create groups of fields that are reusable between multiple forms, use withFieldGroup instead
like-gold
like-goldOP2w ago
That's what I figured. However why would it not throw the same type error then with just the onChange validator then? Just figured if it would throw a type error for one it would for both.
ugly-tan
ugly-tan2w ago
That's an issue with formOptions rather than withForm. It's a tricky one. TypeScript really doesn't like the implementations we tried so far. The core problem is that it should allow:
const sharedOpts = formOptions({})

useForm(sharedOpts)
withForm({...sharedOpts, render: () => {} })
const sharedOpts = formOptions({})

useForm(sharedOpts)
withForm({...sharedOpts, render: () => {} })
as well as
const someDefaults = formOptions({})

useForm({...someDefaults, validators: {} })
const someDefaults = formOptions({})

useForm({...someDefaults, validators: {} })
It either becomes too lenient or too strict and breaks one of the two TL;DR formOptions is not working as intended and it's very difficult to fix but we're looking into options to solve it
like-gold
like-goldOP2w ago
Gotcha, sounds good and that makes sense! Mostly just asking as I'm trying to set some standards for the rest of my team that is coming on at start of next year. I'll switch this over to a withFieldGroup though, thanks!
ugly-tan
ugly-tan2w ago
let me know if there's questions / problems with the field groups

Did you find this page helpful?