Skip to content
Source
Sign inSign up
TanStack/form

[v2] Blurring the field with "change" trigger clears errors. #2341

Opennapewnoarnold(GitHub profile, opens in a new tab) opened this issue 7 comments

Describe the bug

When a form has no blur validator blurring any input will clear errors from other triggers

Your minimal, reproducible example

https://stackblitz.com/edit/tanstack-form-fwx1qdqm?file=src%2Frhf-revalidate.tsx

Steps to reproduce

  1. Press submit
  2. Focus any field
  3. Blur the field & observe error dissapearing

Expected behavior

When I specify validator with only one trigger - "change", i want errors to persist blurring.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

every platform, not related to platform

TanStack Form adapter

react-form

TanStack Form version

2.0.0-alpha.1

TypeScript version

6.0.3

Additional context

No response

  1. napewnoarnold(GitHub profile, opens in a new tab)commented

    In the blog example https://tanstack.com/blog/announcing-tanstack-form-v2-alpha#conditional-validators v1 and v2 examples are not equivalent because revalidateLogic() will not clear the error on blur

  2. LeCarbonator(GitHub profile, opens in a new tab) added labelsscope: core

  3. LeCarbonator(GitHub profile, opens in a new tab) added labelsarea: runtime

  4. LeCarbonator(GitHub profile, opens in a new tab) added labelstype: undecided

  5. LeCarbonator(GitHub profile, opens in a new tab)commented

    Right, so this is actually a different process happening here: Stale error clearing.

    Basically, v2 does the following checks:

    1. Is the current event a client interaction? (This currently includes change and blur)
    2. Are there errors from stale events present? (submit, mount and server. Reasoning is that it's not clear whether the error is still relevant after a stable event like change)
    3. Is the validator not going to validate in this process? (Don't eagerly remove an error if the validator was going to fire again anyways)

    For the reproduction, a validator that fires on change and submit, all three checks pass:

    • Caused by blur
    • Error came from submit
    • The validator isn't firing on blur

    If all three checks pass, the error is removed. So really, this brings up the question of:

    • Is blurring a field an active choice by the user?
    • Should it be considered an interaction?

    I assume by your description and expectation, you'd say that only change should clear the stale errors?

  6. napewnoarnold(GitHub profile, opens in a new tab)commented

    I think yes, only change should clear errors. I see value change as reason to validate form, and blur as a helper event to hide early errors. So blurring shouldn't clear the errors, or trigger validators, only change the "meta" state of the field, and it's up to the developer to draw ui that will hide errors until that field blurred.

    As a developer when i write something like this:

      const form = useForm({
        defaultValues: {
          firstName: '',
          lastName: '',
        },
        validators: [{ run: schema, triggers: ['change'] }],
        onSubmit: async ({ value }) => {
          console.log(value);
        },
      });

    i am not expectin anything when blur event fires.

    --

    As a user blurring unchanged field could mislead that the value inside is no longer incorrect.

    --

    Now that i think about it maybe if state.errors contained errors produced by the defaultValues instantly on mount, that would feel like the pattern of "ui as a function of state". But i have no clue how achievable that is.

    --

    Is the current event a client interaction?

    Yes, but i think the question should be "did the value change?" So only change event - the form library change event.

  7. Well, that can be expanded though. By that logic,

      const form = useForm({
        defaultValues: {
          firstName: '',
          lastName: '',
        },
        validators: [{ run: schema, triggers: [] }],
        onSubmit: async ({ value }) => {
          console.log(value);
        },
      });

    Only runs on submit, but I bet you'd still expect change to clear the error.

    Anyways, sort of unrelated to the blur. We'll discuss the stale vs. interaction event stuff, so until that's resolved, the issue'll stay open.

  8. I've been thinking about this too much lately to objectivly say what i'd expect 😄 Plese do share what you decide and why, would love to hear about it.

  9. I originally created this issue because I couldn't get the configuration I wanted for when errors appear under branded components. The latest version of alpha added a global errorVisibility option, which solves just that.

    I still believe there is some merit in blur not clearing the stale error, but since this issue no longer has an actual real problem behind it, it can probably be closed.

  10. We had a second report of the behaviour, so this is still a relevant issue. Might take a week or so to get addressed.

Sign in to join this conversation.