Skip to content

Commit b3d847d

Browse files
authored
FIX: update field initial value if form has no provided one closes #3874 (#3875)
1 parent 061e05e commit b3d847d

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

packages/vee-validate/src/useForm.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ export function useForm<TValues extends Record<string, any> = Record<string, any
633633
function stageInitialValue(path: string, value: unknown, updateOriginal = false) {
634634
setInPath(formValues, path, value);
635635
setFieldInitialValue(path, value);
636-
if (updateOriginal) {
636+
if (updateOriginal && !opts?.initialValues) {
637637
setInPath(originalInitialValues.value, path, deepCopy(value));
638638
}
639639
}

packages/vee-validate/tests/FieldArray.spec.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -849,3 +849,47 @@ test('clean up form registration on unmount', async () => {
849849
await flushPromises();
850850
expect(1).toBe(1);
851851
});
852+
853+
// #3874
854+
test('adding or removing fields should update form dirty correctly', async () => {
855+
mountWithHoc({
856+
setup() {
857+
const initialValues = {
858+
users: [''],
859+
};
860+
861+
return {
862+
initialValues,
863+
};
864+
},
865+
template: `
866+
<VForm v-slot="{ meta }" :initial-values="initialValues">
867+
<FieldArray name="users" v-slot="{ remove, fields, push }">
868+
<fieldset v-for="(field, idx) in fields" :key="field.key">
869+
<legend>User #{{ idx }}</legend>
870+
<label :for="'name_' + idx">Name</label>
871+
<Field :id="'name_' + idx" :name="'users[' + idx + ']'" />
872+
873+
</fieldset>
874+
<button id="push" type="button" @click="push('')"></button>
875+
<button id="remove" type="button" @click="remove(1)"></button>
876+
</FieldArray>
877+
878+
<pre id="dirty">{{ meta.dirty }}</pre>
879+
</VForm>
880+
`,
881+
});
882+
883+
await flushPromises();
884+
const pushBtn = document.querySelector('#push') as HTMLElement;
885+
const removeBtn = document.querySelector('#remove') as HTMLElement;
886+
const dirty = document.querySelector('#dirty') as HTMLElement;
887+
888+
expect(dirty?.textContent).toBe('false');
889+
pushBtn.click();
890+
await flushPromises();
891+
expect(dirty.textContent).toBe('true');
892+
removeBtn.click();
893+
await flushPromises();
894+
expect(dirty.textContent).toBe('false');
895+
});

0 commit comments

Comments
 (0)