T
TanStack•16mo ago
absent-sapphire

Trigger error in specific field from the onSubmit function

When I submit my form, my backend api can return an error if the sourceName field is already taken. How can I set this error in this field from the onSubmit fn ?
const { Field, Subscribe, ...form } = useForm({
onSubmit: async ({value}) => {
try {
await createSource(value.sourceName)
handleClose()
} catch ({errors}) {
console.log("ERROR _> ", errors)

if(errors.source) {
// trigger set error message in the sourceName field
}
}
},
defaultValues: {
sourceName
},
})
const { Field, Subscribe, ...form } = useForm({
onSubmit: async ({value}) => {
try {
await createSource(value.sourceName)
handleClose()
} catch ({errors}) {
console.log("ERROR _> ", errors)

if(errors.source) {
// trigger set error message in the sourceName field
}
}
},
defaultValues: {
sourceName
},
})
3 Replies
fair-rose
fair-rose•16mo ago
This feature is WIP. I just need to review the PR and we can merge it
absent-sapphire
absent-sapphireOP•16mo ago
Thanks, can I have the link of the related feature please ? So I can follow the thread
absent-sapphire
absent-sapphireOP•16mo ago
GitHub
feat: set field errors from the form validators by fulopkovacs · Pu...
This PR adds the ability to set field errors from the form validators. The API would look like this (this is a value returned by a validator): { form: "General form error", // optional,...

Did you find this page helpful?