Skip to content

Missing error when validating via form options instead of via field #678

Closed

Description

@bennettdams

Describe the bug

When using the (Zod) validator on the form options (passing to useForm) instead of using it directly on the field, there is no entry in the errors/touchedErrors array.

Works (directly on field)

image

Doesn't work (passing to useForm)

image

Your minimal, reproducible example

https://codesandbox.io/p/devbox/nifty-voice-rzmgm9?file=%2Fsrc%2Findex.tsx

Steps to reproduce

There are two forms in the repro example.
The first form uses a validator on the form.Field. Everything works as expected.
The second form uses a validator on the form options. Now, there is no error.

Expected behavior

Validation errors are put on the errors/touchedErrors field, no matter where validation took place.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

TanStack Form adapter

react-form

TanStack Form version

v0.19.1

TypeScript version

v5.4.5

Additional context

No response

Activity

  1. crutchcorn commented on Apr 17, 2024

    @crutchcorn
    Contributor

    Away from a computer right now. Are you checking against the field errors or the form errors. This is a bit confusing, but form validation like this doesn't set field errors, it sets form errors

  2. bennettdams commented on Apr 17, 2024

    @bennettdams
    ContributorAuthor

    Are you checking against the field errors or the form errors.

    Yep, was using the FieldInfo helper from the examples.

    it sets form errors

    You're right, the validation message exists on field.form.state.errors, thanks!

    This is a bit confusing

    I have to agree that this is confusing, as one would always need to look for errors in multiple places.
    Also, it seems that the form has no touchedErrors like a Field has?

    Helper component for reference:

    function FieldInfo({ field }: { field: FieldApi<any, any, any, any> }) {
      // This is empty when using the "form" validator (but not for the "field" validator)
      console.log("field touchedErrors", field.state.meta.touchedErrors);
      // ...but not empty for the form's `errors`
      console.log("form errors", field.form.state.errors);
      return (
        <>
          {field.state.meta.touchedErrors.length === 0 ? (
            "No errors"
          ) : field.state.meta.touchedErrors ? (
            <em>{field.state.meta.touchedErrors}</em>
          ) : null}
        </>
      );
    }
  3. crutchcorn commented on Apr 18, 2024

    @crutchcorn
    Contributor

    FWIW we're actually planning on removing touchedErrors 😅

    Also, we're gonna make the story of form->field validation easier here:

    #656

    Closing as both are a WIP, but thanks for the feedback and detailed report 😇

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions