Is it possible to receive field errors from onSubmit failure responses? #623
-
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: 4 comments 11 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.
Good question @lnhrdt! (And sincere thanks for the patient tone with us as we figure out how to communicate this)
So if we think about how onSubmit is structured internally the code looks something like:
Because of this, the idea is that all errors should be passed through our validators and only the successful logic should be in onSubmit
To be clear; we can still use your server's validation using the new feature + onSubmitAsync to handle the error setting internally for you rather than having to do things manually