Skip to content
Source
Sign inSign up
TanStack/form

Fields marked as valid when there is an invalid field #1149

Closedtmcdo1(GitHub profile, opens in a new tab) opened this issue 6 comments

Describe the bug

isFieldsValid is marked as true when validation hasn't run on another field. Not sure if this is desired behavior but something seems smelly around this.

Working on a dynamic form that renders a field based on another. One of the fields drives another that is hidden or shown depending on the validity. In this example, when a user enters a zip code, we make an API call to fetch the counties associated with it.

  • If there are none, the zip code is invalid.
  • If there is only one county, we want to set the county on the form and mark both the county and zip code values as valid.
  • If there is more than one county, render a select field and mark the first option as the selected county

In implementing this, the dynamic county field is technically not valid initially but isFieldsValid is true even before the zip code validation logic is completed.

It's worth noting that validating a specific field doesn't seem to clear the error at the form level, which means the whole form needs to be validated (comments in example).

I found that onChangeListenTo didn't make 100% sense for this or the listeners since the field is dynamically rendered.

Your minimal, reproducible example

https://stackblitz.com/edit/vitejs-vite-ronky49r?file=src%2FApp.tsx

Steps to reproduce

  1. open example
  2. enter two letters for the "State" field (e.g. "CA")
  3. enter a zipcode (e.g. "77001")

Notice how the form is marked as invalid but the fields are marked as valid.

Expected behavior

I would expect that fields would be invalid if the county input hasn't been validated yet

How often does this bug happen?

Every time

Screenshots or Videos

https://github.com/user-attachments/assets/97405414-704c-44a2-9fd6-a05178078212

Platform

  • OS: MacOS
  • Browser: Arc (Chromium)

TanStack Form adapter

react-form

TanStack Form version

0.42.0

TypeScript version

5.7.2

Additional context

No response

  1. Is this similar to #1140 (comment)? I mean using isValidating to cover this case.

    Seems similar but the state remains the same even after the validation check is run. The form also starts as invalid after the mount validation, so the county field validation hasn't even started yet for isValidating to be marked true for that specific field.

  2. If you noticed separate bugs could you please file separate issues? It's a bit confusing otherwise :)

    Neither manually setting a field with form.setFieldValue nor manually validating a field with form.validateField are validating the form, that's intended. As you also noticed you can run form.validate('change') to achieve the desired result.

    What is still missing?

  3. If you noticed separate bugs could you please file separate issues? It's a bit confusing otherwise :)

    Sure thing, my apologies

    Neither manually setting a field with form.setFieldValue nor manually validating a field with form.validateField are validating the form, that's intended

    I understand form.setFieldValue not validating the form but I am curious why form.validateField wouldn't remove an error stored in the error map at the form level for that specific field. In this scenario, the form level still has the "County is required" error even though it has been set and form.validateField has been run on it.

    I can create a separate issue for this if you would like, since it's a different potential problem. Maybe I don't understand why one would use form.validateField and not have the form be valid afterwards if all the other fields are valid.

    What is still missing?

    I think the primary problem for this specific issue that I am seeing is that the fields are being marked as valid, even though validation failed for one of the fields ("county" in this example). Note the "County is required" in the errorMap.

    Image

    Sorry if I'm not understanding the nuance between the different properties. Appreciate the questions.

  4. Sounds like form not clearing stale errors such as the one for country is related to #999, I got a PR out for this right now:

    #1157

  5. This should be solved since #1157 got merged. Please let us know if that is not the case

Sign in to join this conversation.