-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
fix: preserve dirty/touched meta on late mount (#5072) #5145
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "vee-validate": patch | ||
| --- | ||
|
|
||
| Preserve dirty meta when field component mounts after programmatic changes (#5072) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -159,7 +159,15 @@ export function _useFieldValue<TValue = unknown>( | |
| // prioritize model value over form values | ||
| // #3429 | ||
| const currentValue = resolveModelValue(modelValue, form, initialValue, path); | ||
| form.stageInitialValue(unref(path), currentValue, true); | ||
| // Skip staging if the path already has an initial value on the form and no explicit | ||
| // modelValue was provided to this field. This preserves dirty state when a field | ||
| // component mounts after the value was changed programmatically (e.g., via setFieldValue). | ||
| // Without this check, stageInitialValue would overwrite the original initial value with | ||
| // the current (dirty) value, making dirty=false (#5072). | ||
| const existingInitial = getFromPath(form.initialValues.value, unref(path)); | ||
| if (existingInitial === undefined || modelValue !== undefined) { | ||
| form.stageInitialValue(unref(path), currentValue, true); | ||
| } | ||
|
Comment on lines
+167
to
+170
|
||
| // otherwise use a computed setter that triggers the `setFieldValue` | ||
| const value = computed<TValue>({ | ||
| get() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -287,11 +287,15 @@ export function useForm< | |
| UNSET_BATCH.splice(unsetBatchIndex, 1); | ||
| } | ||
|
|
||
| // Preserve touched state from an existing path state that was created | ||
| // before this field component mounted (e.g., via setFieldValue) (#5072) | ||
| const existingTouched = pathStateExists ? pathStateExists.touched : false; | ||
|
|
||
| const id = FIELD_ID_COUNTER++; | ||
| const state = reactive({ | ||
| id, | ||
| path, | ||
| touched: false, | ||
| touched: existingTouched, | ||
|
Comment on lines
+290
to
+298
|
||
| pending: false, | ||
| valid: true, | ||
| validated: !!initialErrors[pathValue]?.length, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changeset note mentions preserving dirty meta, but this PR also changes touched preservation on late mount. Consider updating the changeset description to reflect both behaviors so the release note matches the shipped fix.