Skip to content
Source
Sign inSign up
TanStack/form

Field submit error is removed on field blur without a change #1242

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

Describe the bug

A submit error is removed on blur of a field when the value didn't change.

Your minimal, reproducible example

https://codesandbox.io/p/devbox/hungry-sanderson-ycs6rd

Steps to reproduce

  1. Press submit without entering a name
  2. Focus the name input field
  3. Leave focus without a change

Expected behavior

The error which occured on submit is not removed on blur when the value during focus didn't change.

How often does this bug happen?

Every time

Screenshots or Videos

No response

Platform

  • OS: WIndows
  • Browser: Chrome

TanStack Form adapter

react-form

TanStack Form version

1.0.2

TypeScript version

No response

Additional context

No response

  1. From what I find, the onChange validator will rightly handle the blurring by maintaining errors in the state.meta.errors. onSubmit however, is causing the blur event to clear the errors.

    I might be wrong but I think this is where the problem is inFieldApi.ts:

    /**
     *  when we have an error for onSubmit in the state, we want
     *  to clear the error as soon as the user enters a valid value in the field
     */
    const submitErrKey = getErrorMapKey('submit')
    
    if (
      this.state.meta.errorMap[submitErrKey] &&
      cause !== 'submit' &&
      !hasErrored
    ) {
      this.setMeta((prev) => ({
        ...prev,
        errorMap: {
          ...prev.errorMap,
          [submitErrKey]: undefined,
        },
      }))
    }

    Since blur's cause is not submit, but rather "change" in the case where we blur out of a field after submitting, it won't remap the hasErrored or the errorMap. I don't know if this is by design so I will let a maintainer comment on this.

  2. LeCarbonator(GitHub profile, opens in a new tab) added labelsv2: needs investigation

  3. I believe this is intended design for v1, though we're open for discussion on how v2 should tackle stale errors. What events should clear them, which error sources should be considered etc. etc.

    As for v2 alpha 0,

    • Both change and blur clear stale error sources
    • Stale error sources include mount, submit and server (from server-side rendering).
  4. LeCarbonator(GitHub profile, opens in a new tab) removed labelsv2: needs investigation

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

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

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

Sign in to join this conversation.