TanStackT
TanStack3w ago
43 replies
moderate-tomato

Setting custom errors for form fields

I am using the useAppForm hook, I wanted to know how can I set custom errors on each field once the form is submitted and I get response from my API.

const form = useAppForm<z.infer<typeof loginSchema>>({
  defaultValues: { email: "", password: "" },
  validators: { onSubmit: loginSchema },
  onSubmit: async ({ value: data }) => {
    const { ok, error } = await loginFn({ data });

    if (ok) {
      await navigate({ to: "/" });
      return;
    }
  },
});


Here the error that is returned from the loginFn is an object with email and password fields. I want to set those error messages on the respective fields.
Was this page helpful?