Fields marked as valid when there is an invalid field #1149
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#
- open example
- enter two letters for the "State" field (e.g. "CA")
- 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
Is this similar to #1140 (comment)? I mean using
isValidatingto 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
isValidatingto be marked true for that specific field.If you noticed separate bugs could you please file separate issues? It's a bit confusing otherwise :)
Neither manually setting a field with
form.setFieldValuenor manually validating a field withform.validateFieldare validating the form, that's intended. As you also noticed you can runform.validate('change')to achieve the desired result.What is still missing?
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.setFieldValuenor manually validating a field withform.validateFieldare validating the form, that's intendedI understand
form.setFieldValuenot validating the form but I am curious whyform.validateFieldwouldn'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 andform.validateFieldhas 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.validateFieldand 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.
Sorry if I'm not understanding the nuance between the different properties. Appreciate the questions.
This should be solved since #1157 got merged. Please let us know if that is not the case
crutchcorn(GitHub profile, opens in a new tab) closed this
Sign in to join this conversation.
Is this similar to https://github.com/TanStack/form/issues/1140#issuecomment-2640639290? I mean using
isValidatingto cover this case.