While discussing items we want to improve for a future v2, validation around onSubmit has come up a number of times.
As such, we're going to be backporting an API coming in v2 to the current version of Form (v1):
useForm({
onSubmit: async ({ value, createValidationError }) => {
try {
await callEndpoint(value)
navigateToOverview()
} catch (error) {
if (error.code === 400) {
return createValidationError({ form: `The endpoint had an issue: ${error.message}` })
}
}
}
})
This work needs https://github.com/TanStack/form/pull/2128 to be merged first to avoid headaches with generics, but that should come shortly.
We want to hear from you if this API works/doesn't work for your needs and, if not, why not.
Thanks all!
Hello @crutchcorn thank you for creating this issue 🙌
Quick question: This also allows field-level validation, correct? With the correct field types.
Something like:
Also, another point raised in another discussion brought up a good point about
isSubmitSuccessful. SinceonSubmitwouldn't throw in this case,isSubmitSuccessfulwould be marked astrue(source).Perhaps this was already going to be taken into consideration, wanted to flag it just in case.