fix(forms): cleanup dynamically removed form fields#8572
Open
Trandx wants to merge 1 commit into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix dynamic field cleanup in forms
Related issue: #8408
Problem
When dynamically removing form fields using
v-if,v-for, or custom dynamic arrays, the field state was not properly cleaned up from the internal form store.Although the component was destroyed, the following data remained registered internally:
_states)This caused several issues:
Example
When
visiblebecomesfalse, the component is unmounted visually, but the internal form state still contains:As a result:
The same issue also affects dynamic arrays:
Removing an item from the array leaves stale field states and watchers behind.
Root Cause
defineField()registers field state and watchers internally, but there was no cleanup mechanism when a field component was unmounted.As a result, dynamically removed fields remained inside:
Solution
This PR introduces a new
unregister(field)cleanup method inuseForm.The method now:
Field components automatically call
unregister()duringbeforeUnmount.Result
Dynamic form fields are now properly cleaned up when removed.
This fixes:
and significantly improves dynamic form reliability.
Test Reproduction
https://stackblitz.com/edit/vitejs-vite-pub96hsp