-
I've been exploring adopting TanStack Form in my application. In our architecture, all form validation is done on the server, through submission. If the request is valid, the request succeeds. If the request is invalid, it returns a response of field errors (one or more). I haven't found a way to adapt this architecture to TanStack Form. Is there a way to parse the response from an onSubmit failure (either thrown error or rejected promise) and apply the result to field errors? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 31 replies
-
TL;DR Currently, field errors can be set only from the field's validators, but we're planning to change this in the upcoming weeks. Form validation on the server can be achieved using the form's You can validate the fields on the server by sending a fetch request in the field's <form.Field
name="age"
validators={{
onSubmitAsync: async ({value}) => {
const below13: boolean = await validateAgeOnServer(value)
return below13 ? 'You must be 13 to make an account' : undefined,
}
}}
>{...}</form.Field> That's just what's currently possible, but your question sparked a conversation between the maintainers and we decided to implement a way to set field-specific errors from the form's validators. We already agreed on the API, and I picked up the task. I'm planning to do it in the upcoming weeks, but it's probably a tricky one, so please bear with me! 😅 I'm planning to ship it before v1. |
Beta Was this translation helpful? Give feedback.
-
I'm facing a similar issue. Waiting for the release of the improvement :) |
Beta Was this translation helpful? Give feedback.
-
For those struggling with setting field-specific errors from onSubmit: async ({ value }) => {
if (someCondition) {
setFieldMeta("fieldName", (prev) => ({
...prev,
errorMap: {
onChange: "Your error message here",
},
}));
return;
}
// Rest of your submit logic
} This method allows you to set custom error messages for specific fields when handling submission failures or server-side validation responses. It's not ideal, but it works as a temporary solution until an official API is implemented for this use case. |
Beta Was this translation helpful? Give feedback.
-
@Ali-Albaker and everyone else interested in this issue: now that #656 has been merged, we have an official API for setting field-level errors from the form's validators. Here's the example in the docs. This is how @d3vzr's workaround would look like using the official APIs: validators: {
// Note that this is the onSubmit validator
onSubmit: async ({ value }) => {
if (someCondition) {
return {
fields: {
fieldName: "Your error message here",
}
}
}
return null
}
} You can read more about setting field-level errors in the form's validators in the docs: https://tanstack.com/form/latest/docs/framework/react/guides/validation#setting-field-level-errors-from-the-forms-validators |
Beta Was this translation helpful? Give feedback.
-
What's the way to integrate with Query's |
Beta Was this translation helpful? Give feedback.
-
All this definitely needs to be documented somewhere. Stumbled onto this thread while trying to deal with the same issues. Is there way to make the response from a successul submission via |
Beta Was this translation helpful? Give feedback.
-
I'm gonna handle forms myself. This is too much noise. |
Beta Was this translation helpful? Give feedback.
Hey everyone! First, huge thanks to everyone volunteering time for TanStack Form (especially @fulopkovacs for the #656 solution and @crutchcorn for the overall design).
I originally kicked off this discussion because we needed to submit form data to an API that returns per-field validation errors, and it wasn't obvious how to feed those errors back into the form state. A year later people are still landing here confused, so I'm summarizing the two patterns you can use today and will make this the new accepted answer to help folks find it.
1. validators.onSubmitAsync (my go-to)