Skip to content

Commit 804ec6f

Browse files
committed
fix: use flags to avoid validating during reset #4404 #4467
1 parent 2bda716 commit 804ec6f

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

.changeset/short-points-attack.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': patch
3+
---
4+
5+
fix: use flags to avoid validating during reset #4404 #4467

packages/vee-validate/src/types/forms.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ export interface PathState<TValue = unknown> {
9999
fieldsCount: number;
100100
__flags: {
101101
pendingUnmount: Record<string, boolean>;
102+
pendingReset: boolean;
102103
};
103104
validate?: FieldValidator;
104105
}

packages/vee-validate/src/useField.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,12 @@ function _useField<TValue = unknown>(
155155
const errorMessage = computed(() => errors.value[0]);
156156

157157
if (syncVModel) {
158-
useVModel({ value, prop: syncVModel, handleChange, validateOnModelUpdate: !!opts?.validateOnValueUpdate });
158+
useVModel({
159+
value,
160+
prop: syncVModel,
161+
handleChange,
162+
shouldValidate: () => validateOnValueUpdate && !flags.pendingReset,
163+
});
159164
}
160165

161166
/**
@@ -554,10 +559,10 @@ interface ModelOpts<TValue> {
554559
prop: string | boolean;
555560
value: Ref<TValue>;
556561
handleChange: FieldContext['handleChange'];
557-
validateOnModelUpdate: boolean;
562+
shouldValidate: () => boolean;
558563
}
559564

560-
function useVModel<TValue = unknown>({ prop, value, handleChange, validateOnModelUpdate }: ModelOpts<TValue>) {
565+
function useVModel<TValue = unknown>({ prop, value, handleChange, shouldValidate }: ModelOpts<TValue>) {
561566
const vm = getCurrentInstance();
562567
/* istanbul ignore next */
563568
if (!vm || !prop) {
@@ -595,7 +600,7 @@ function useVModel<TValue = unknown>({ prop, value, handleChange, validateOnMode
595600
return;
596601
}
597602

598-
handleChange(newValue, validateOnModelUpdate);
603+
handleChange(newValue, shouldValidate());
599604
},
600605
);
601606
}

packages/vee-validate/src/useFieldState.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export function useFieldState<TValue = unknown>(
6363
value,
6464
initialValue,
6565
meta,
66-
flags: { pendingUnmount: { [id]: false } },
66+
flags: { pendingUnmount: { [id]: false }, pendingReset: false },
6767
errors,
6868
setState,
6969
};

packages/vee-validate/src/useForm.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ export function useForm<
309309
multiple: false,
310310
__flags: {
311311
pendingUnmount: { [id]: false },
312+
pendingReset: false,
312313
},
313314
fieldsCount: 1,
314315
validate: config?.validate,
@@ -733,11 +734,21 @@ export function useForm<
733734

734735
function resetField(field: Path<TValues>, state?: Partial<FieldState>) {
735736
const newValue = state && 'value' in state ? state.value : getFromPath(initialValues.value, field);
737+
const pathState = findPathState(field);
738+
if (pathState) {
739+
pathState.__flags.pendingReset = true;
740+
}
736741

737742
setFieldInitialValue(field, deepCopy(newValue));
738743
setFieldValue(field, newValue as PathValue<TValues, typeof field>, false);
739744
setFieldTouched(field, state?.touched ?? false);
740745
setFieldError(field, state?.errors || []);
746+
747+
nextTick(() => {
748+
if (pathState) {
749+
pathState.__flags.pendingReset = false;
750+
}
751+
});
741752
}
742753

743754
/**
@@ -748,6 +759,7 @@ export function useForm<
748759
newValues = isTypedSchema(schema) && isCallable(schema.cast) ? schema.cast(newValues) : newValues;
749760
setInitialValues(newValues);
750761
mutateAllPathState(state => {
762+
state.__flags.pendingReset = true;
751763
state.validated = false;
752764
state.touched = resetState?.touched?.[state.path as Path<TValues>] || false;
753765

@@ -760,6 +772,10 @@ export function useForm<
760772
submitCount.value = resetState?.submitCount || 0;
761773
nextTick(() => {
762774
validate({ mode: 'silent' });
775+
776+
mutateAllPathState(state => {
777+
state.__flags.pendingReset = false;
778+
});
763779
});
764780
}
765781

0 commit comments

Comments
 (0)