TanStackT
TanStack13mo ago
3 replies
worthy-rose

Possible to update Zod schema and revalidate?

I have a toggle that makes an additional field required. I only want to validate it when the toggle is on. So I'm trying to merge in the additional Zod schema and validate against it.

For this I have a useEffect (pictured) to listen for the toggle to change in form.store and merge an additional Zod schema. And it works! ...when I toggle it on. My otherwise valid form goes to canSubmit: false.

However if I turn the toggle off it should be going back to the initial schema and validate the form. However this does not happen: the form stays in canSubmit: false and the fieldMeta for the unrequired field still displays an error.

Am I thinking about this correctly? Should it be possible to swap out the schema and validate the form? Do I need to revalidate the whole form when I swap out the schema? Since the schema is changed by setState this should cause a rerender and a revalidate?
Many many thanks, I've spent too many hours sweating over this. 🥹
Screenshot_2024-12-18_214807.png
Was this page helpful?