Skip to content

Commit

Permalink
fix: use flags to avoid validating during reset #4404 #4467
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Sep 9, 2023
1 parent 2bda716 commit 804ec6f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 5 deletions.
5 changes: 5 additions & 0 deletions .changeset/short-points-attack.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vee-validate': patch
---

fix: use flags to avoid validating during reset #4404 #4467
1 change: 1 addition & 0 deletions packages/vee-validate/src/types/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export interface PathState<TValue = unknown> {
fieldsCount: number;
__flags: {
pendingUnmount: Record<string, boolean>;
pendingReset: boolean;
};
validate?: FieldValidator;
}
Expand Down
13 changes: 9 additions & 4 deletions packages/vee-validate/src/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,12 @@ function _useField<TValue = unknown>(
const errorMessage = computed(() => errors.value[0]);

if (syncVModel) {
useVModel({ value, prop: syncVModel, handleChange, validateOnModelUpdate: !!opts?.validateOnValueUpdate });
useVModel({
value,
prop: syncVModel,
handleChange,
shouldValidate: () => validateOnValueUpdate && !flags.pendingReset,
});
}

/**
Expand Down Expand Up @@ -554,10 +559,10 @@ interface ModelOpts<TValue> {
prop: string | boolean;
value: Ref<TValue>;
handleChange: FieldContext['handleChange'];
validateOnModelUpdate: boolean;
shouldValidate: () => boolean;
}

function useVModel<TValue = unknown>({ prop, value, handleChange, validateOnModelUpdate }: ModelOpts<TValue>) {
function useVModel<TValue = unknown>({ prop, value, handleChange, shouldValidate }: ModelOpts<TValue>) {
const vm = getCurrentInstance();
/* istanbul ignore next */
if (!vm || !prop) {
Expand Down Expand Up @@ -595,7 +600,7 @@ function useVModel<TValue = unknown>({ prop, value, handleChange, validateOnMode
return;
}

handleChange(newValue, validateOnModelUpdate);
handleChange(newValue, shouldValidate());
},
);
}
Expand Down
2 changes: 1 addition & 1 deletion packages/vee-validate/src/useFieldState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ export function useFieldState<TValue = unknown>(
value,
initialValue,
meta,
flags: { pendingUnmount: { [id]: false } },
flags: { pendingUnmount: { [id]: false }, pendingReset: false },
errors,
setState,
};
Expand Down
16 changes: 16 additions & 0 deletions packages/vee-validate/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ export function useForm<
multiple: false,
__flags: {
pendingUnmount: { [id]: false },
pendingReset: false,
},
fieldsCount: 1,
validate: config?.validate,
Expand Down Expand Up @@ -733,11 +734,21 @@ export function useForm<

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

setFieldInitialValue(field, deepCopy(newValue));
setFieldValue(field, newValue as PathValue<TValues, typeof field>, false);
setFieldTouched(field, state?.touched ?? false);
setFieldError(field, state?.errors || []);

nextTick(() => {
if (pathState) {
pathState.__flags.pendingReset = false;
}
});
}

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

Expand All @@ -760,6 +772,10 @@ export function useForm<
submitCount.value = resetState?.submitCount || 0;
nextTick(() => {
validate({ mode: 'silent' });

mutateAllPathState(state => {
state.__flags.pendingReset = false;
});
});
}

Expand Down

0 comments on commit 804ec6f

Please sign in to comment.