TanStackT
TanStack2y ago
3 replies
popular-magenta

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
    },
  })
Was this page helpful?