Skip to content
Source
Sign inSign up
TanStack/form

Introduce an easy way to create errors from `onSubmit` #2188

Send to my agent
Opencrutchcorn(GitHub profile, opens in a new tab) opened this issue 7 comments

While discussing items we want to improve for a future v2, validation around onSubmit has come up a number of times.

As such, we're going to be backporting an API coming in v2 to the current version of Form (v1):

useForm({
   // ...
   onSubmit: async ({ value, createValidationError }) => {
     try {
        await callEndpoint(value)
        navigateToOverview()
     } catch (error) {
        if (error.code === 400) {
           return createValidationError({ form: `The endpoint had an issue: ${error.message}` }) 
        }
     }
   }
})

This work needs https://github.com/TanStack/form/pull/2128 to be merged first to avoid headaches with generics, but that should come shortly.

We want to hear from you if this API works/doesn't work for your needs and, if not, why not.

Thanks all!

  1. crutchcorn(GitHub profile, opens in a new tab) assignedLeCarbonator

  2. crutchcorn(GitHub profile, opens in a new tab) added labelstype: enhancement

  3. Hello @crutchcorn thank you for creating this issue 🙌

    Quick question: This also allows field-level validation, correct? With the correct field types.

    Something like:

    createValidationError({
      form: `The endpoint had an issue: ${error.message}`,
      fields: {
        name: "Field-level validation is also possible"
      },
    }) 

    Also, another point raised in another discussion brought up a good point about isSubmitSuccessful. Since onSubmit wouldn't throw in this case, isSubmitSuccessful would be marked as true (source).

    Perhaps this was already going to be taken into consideration, wanted to flag it just in case.

  4. Yes, that is the plan @joaopslins .

    Also, isSubmitSuccessful will be false, to mirror what happens when a validation failed during the submission process.

  5. Hello,

    Is there a timeline on when this would be available ? :).

  6. There's no strict timeline for it yet. I tinkered a bit, but there's some type shenanigans to do to make it work for the usual React cases. The way it's looking now, it'll probably be a very constrained usage - Only certain error objects allowed.

  7. Hi,

    Any updates on this. dealing with a gnarly form

  8. There's no strict timeline for it yet. I tinkered a bit, but there's some type shenanigans to do to make it work for the usual React cases. The way it's looking now, it'll probably be a very constrained usage - Only certain error objects allowed.

    Will this constrained usage only apply in the v1 backport?

  9. @andycochrane Error types are slightly more constrained in v2, so it doesn't really apply. String errors get normalized to { message: string }.

    Adding it to v2 was trivial due to that, but v1 allows any type of error. That's why the backport is delayed the way it is, there's type stuff that needs to be ironed out.

    Once the PR is out, it'll reference this issue.

Sign in to join this conversation.