From a11cd253e5ddd3eb5340cf03f5ae2dd1c89f2e4e Mon Sep 17 00:00:00 2001 From: Abdelrahman Awad Date: Tue, 24 Aug 2021 00:38:09 +0200 Subject: [PATCH] fix: priotrize model value over form path value closes #3429 --- packages/vee-validate/src/useField.ts | 25 +++++++++++-------------- 1 file changed, 11 insertions(+), 14 deletions(-) diff --git a/packages/vee-validate/src/useField.ts b/packages/vee-validate/src/useField.ts index e980e653f..6d42f87ff 100644 --- a/packages/vee-validate/src/useField.ts +++ b/packages/vee-validate/src/useField.ts @@ -101,7 +101,7 @@ export function useField( checked, } = useValidationState({ name, - initValue: initialValue, + modelValue: initialValue, form, type, checkedValue, @@ -337,7 +337,7 @@ function normalizeOptions(name: string, opts: Partial({ name, - initValue, + modelValue, form, type, checkedValue, @@ -345,7 +345,7 @@ function useValidationState({ }: { name: MaybeRef; checkedValue?: MaybeRef; - initValue?: MaybeRef; + modelValue?: MaybeRef; form?: PrivateFormContext; type?: string; standalone?: boolean; @@ -353,18 +353,13 @@ function useValidationState({ const { errors, errorMessage, setErrors } = useFieldErrors(name, form); const formInitialValues = standalone ? undefined : injectWithSelf(FormInitialValuesKey, undefined); // clones the ref value to a mutable version - const initialValueSourceRef = ref(unref(initValue)) as Ref; + const initialValueSourceRef = ref(unref(modelValue)) as Ref; const initialValue = computed(() => { return getFromPath(unref(formInitialValues), unref(name), unref(initialValueSourceRef)) as TValue; }); - const value = useFieldValue( - initialValueSourceRef.value === undefined ? initialValue : initialValueSourceRef, - name, - form - ); - + const value = useFieldValue(initialValue, modelValue, name, form); const meta = useFieldMeta(initialValue, value, errors); const checked = hasCheckedAttr(type) @@ -409,7 +404,7 @@ function useValidationState({ const newValue = state && 'value' in state ? (state.value as TValue) - : (getFromPath(unref(formInitialValues), fieldPath, unref(initValue) as TValue) as TValue); + : (getFromPath(unref(formInitialValues), fieldPath, unref(modelValue) as TValue) as TValue); if (form) { form.setFieldValue(fieldPath, newValue, { force: true }); @@ -489,19 +484,21 @@ export function extractRuleFromSchema( */ export function useFieldValue( initialValue: MaybeRef, + modelValue: MaybeRef | undefined, path: MaybeRef, form?: PrivateFormContext ): WritableRef { // if no form is associated, use a regular ref. if (!form) { - return ref(unref(initialValue)) as WritableRef; + return ref(unref(modelValue || initialValue)) as WritableRef; } // to set the initial value, first check if there is a current value, if there is then use it. // otherwise use the configured initial value if it exists. + // prioritize model value over form values // #3429 - const currentValue = getFromPath(form.values, unref(path), unref(initialValue)); - form.stageInitialValue(unref(path), currentValue === undefined ? unref(initialValue) : currentValue); + const currentValue = modelValue ? unref(modelValue) : getFromPath(form.values, unref(path), unref(initialValue)); + form.stageInitialValue(unref(path), currentValue); // otherwise use a computed setter that triggers the `setFieldValue` const value = computed({ get() {