TanStackT
TanStack8mo ago
3 replies
dual-salmon

How to get data from a backend api validation in the onSubmit?

I'm migrating an app from Conform to Tanstack Form, and i had this kind of validation
  const submission = await parseWithZod(formData, {
    schema: (intent) =>
      schema.transform(async (data, ctx) => {
        if (intent !== null) return { ...data, verify: null }

        const { data: verify, error } = await api.POST("/api/auth/sign-up", {
          body: {
            email: data.email,
            verify: {
              targetQueryParam: targetQueryParam,
              typeQueryParam: typeQueryParam,
              url: `${import.meta.env.VITE_BASE_URL}${href("/auth/verify")}`,
            },
          },
        })

        if (error) {
          ctx.addIssue({
            code: z.ZodIssueCode.custom,
            message: error.message,
          })
          return z.NEVER
        }

        return { ...data, verify }
      }),
    async: true,
  })

  if (submission.status !== "success" || !submission.value.verify) {
    return submission.reply()
  }

  const { verify } = submission.value


if the api returned an error i was invalidating the form with the api error message, but if there was no error i was populating the form data with the api returned data, so i can use it later, now im trying to do something like that in the ts form, but i cant find a way to do it
Was this page helpful?