-
-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Field Array Validation Not Refreshing Properly When Field is Removed #629
Comments
Looks like we have a bug here, I'll check it out later this week! |
@crutchcorn You worked a lot on array fields, so I'll summon you to ask for help! 😆 If you check the video recording below, there are subfields in
When we delete Kapture.2024-03-24.at.22.07.36.mp4I could dig deeper, but I thought maybe you could give me some pointers here since you've worked with array fields recently. |
@fulopkovacs do you think this is in part due to this? #662 |
They might be related, but I tested the PR of the supposed fix for #662 (#700), and it seems like it does not solve the issue. I'll look into it! (Thanks to #656, I'm much more comfortable with working on validation-related issues 🤣 ) |
Ok, I found the bug and made a fix locally, I will try to clean it up and commit it this week. The main source of the problem is that the array field is not re-validated after one of its subfields gets removed. But, here are some interesting things that I found when I was investigating the issue:
The fix I have locally deletes the data related to the last subfield from |
Describe the bug
When multiple fields are added to a field array, and there is validation errors, if you update one of these fields to not have a validation error, and remove a field with a validation error (i - 1 to the good field), the removed field's validation error is applied to the field that does not have validation errors. It seems as though validation (or form state) is not properly re-evaluated and updated when a field is removed from a field array. Due to this bug, a call to form.validateAllFields("change") was attempted as a work around, but this results in the following error:
To view this error in the linked example project, uncomment out the indicated line 63 in the Form component.
Your minimal, reproducible example
https://stackblitz.com/edit/vitejs-vite-81fflp?file=src%2Fcomponents%2FForm.tsx
Steps to reproduce
Result is the 2nd person (which was the 3rd person) now has the validation error showing of the previously deleted 2nd person.
To see the error, call form.validateAllFields("change") immediately after removing the field from the field array.
Expected behavior
I expect to see all validation for the 2nd person (previously the 3rd person) to continue to not have any errors, instead I'm seeing the previous validation error for the original 2nd person (which was removed).
When trying to work around the issue by manually revalidating all fields with form.validateAllFields("change") after the field is removed, errors are thrown (see above). This error can be avoided if the form.validateAllFields("change") call is placed in a setTimeout.
How often does this bug happen?
Every time
Screenshots or Videos
No response
Platform
macOS 12.7.2
chrome Version 122.0.6261.129 (Official Build) (arm64)
TanStack Form adapter
react-form
TanStack Form version
v0.16.0
TypeScript version
5.2.2
Additional context
No response
The text was updated successfully, but these errors were encountered: