Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fix-4827-reactive-resetfield.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"vee-validate": patch
---

Fix resetField to use current reactive initial value instead of stale value (#4827)
14 changes: 14 additions & 0 deletions packages/vee-validate/src/useFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,11 @@ export function _useFieldValue<TValue = unknown>(

function resolveInitialValue() {
if (!form) {
// If the original modelValue is a ref, read from it directly to maintain reactivity
if (isRef(modelValue)) {
return modelValue.value as TValue;
}

return unref(modelRef) as TValue;
}

Expand Down Expand Up @@ -160,6 +165,15 @@ export function _useFieldValue<TValue = unknown>(
// #3429
const currentValue = resolveModelValue(modelValue, form, initialValue, path);
form.stageInitialValue(unref(path), currentValue, true);

// If the modelValue is a ref, watch it and update the form's initial value when it changes
// This ensures resetField uses the current reactive initial value (#4827)
if (isRef(modelValue)) {
watch(modelValue, newVal => {
form.setFieldInitialValue(unref(path), newVal as any, true);
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor type-safety: inside the watcher callback newVal should already be typed as TValue, and setFieldInitialValue accepts unknown, so the as any cast is unnecessary and hides potential typing issues. Prefer passing newVal directly (or as TValue if needed).

Suggested change
form.setFieldInitialValue(unref(path), newVal as any, true);
form.setFieldInitialValue(unref(path), newVal, true);

Copilot uses AI. Check for mistakes.
});
Comment on lines +172 to +174
Copy link

Copilot AI Mar 4, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new watch(modelValue, ...) uses default flush timing, so initialValues may not be updated until the next tick. If a consumer does initialValueRef.value = ...; resetField() in the same tick, resetField() can still pick up the stale form initial value. Consider setting the watcher to flush: 'sync' (and likely deep: true for object refs) so the form initial value stays in sync immediately with the reactive ref.

Suggested change
watch(modelValue, newVal => {
form.setFieldInitialValue(unref(path), newVal as any, true);
});
watch(
modelValue,
newVal => {
form.setFieldInitialValue(unref(path), newVal as any, true);
},
{
flush: 'sync',
deep: true,
}
);

Copilot uses AI. Check for mistakes.
}

// otherwise use a computed setter that triggers the `setFieldValue`
const value = computed<TValue>({
get() {
Expand Down
65 changes: 65 additions & 0 deletions packages/vee-validate/tests/useField.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1059,4 +1059,69 @@ describe('useField()', () => {

expect(form.values.field).toEqual('test');
});

// #4827
test('resetField should use current reactive initial value (without form)', async () => {
let field!: FieldContext;
const initialValue = ref('before change');

mountWithHoc({
setup() {
field = useField('field', undefined, { initialValue });

return {};
},
template: '<div></div>',
});

await flushPromises();
expect(field.value.value).toBe('before change');

// Change the reactive initial value
initialValue.value = 'after change';
await flushPromises();

// Change the field's current value to something else
field.value.value = 'some random value';
await flushPromises();

// Reset field should use the new reactive initial value
field.resetField();
await flushPromises();

expect(field.value.value).toBe('after change');
});

// #4827
test('resetField should use current reactive initial value (with form)', async () => {
let field!: FieldContext;
const initialValue = ref('before change');

mountWithHoc({
setup() {
useForm();
field = useField('field', undefined, { initialValue });

return {};
},
template: '<div></div>',
});

await flushPromises();
expect(field.value.value).toBe('before change');

// Change the reactive initial value
initialValue.value = 'after change';
await flushPromises();

// Change the field's current value to something else
field.value.value = 'some random value';
await flushPromises();

// Reset field should use the new reactive initial value
field.resetField();
await flushPromises();

expect(field.value.value).toBe('after change');
});
});
Loading