Skip to content

Commit

Permalink
fix: priotrize model value over form path value closes #3429
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Sep 26, 2021
1 parent 1e98f43 commit a11cd25
Showing 1 changed file with 11 additions and 14 deletions.
25 changes: 11 additions & 14 deletions packages/vee-validate/src/useField.ts
Expand Up @@ -101,7 +101,7 @@ export function useField<TValue = unknown>(
checked,
} = useValidationState<TValue>({
name,
initValue: initialValue,
modelValue: initialValue,
form,
type,
checkedValue,
Expand Down Expand Up @@ -337,34 +337,29 @@ function normalizeOptions<TValue>(name: string, opts: Partial<FieldOptions<TValu
*/
function useValidationState<TValue>({
name,
initValue,
modelValue,
form,
type,
checkedValue,
standalone,
}: {
name: MaybeRef<string>;
checkedValue?: MaybeRef<TValue>;
initValue?: MaybeRef<TValue>;
modelValue?: MaybeRef<TValue>;
form?: PrivateFormContext;
type?: string;
standalone?: boolean;
}) {
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<TValue>;
const initialValueSourceRef = ref(unref(modelValue)) as Ref<TValue>;

const initialValue = computed(() => {
return getFromPath<TValue>(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)
Expand Down Expand Up @@ -409,7 +404,7 @@ function useValidationState<TValue>({
const newValue =
state && 'value' in state
? (state.value as TValue)
: (getFromPath<TValue>(unref(formInitialValues), fieldPath, unref(initValue) as TValue) as TValue);
: (getFromPath<TValue>(unref(formInitialValues), fieldPath, unref(modelValue) as TValue) as TValue);

if (form) {
form.setFieldValue(fieldPath, newValue, { force: true });
Expand Down Expand Up @@ -489,19 +484,21 @@ export function extractRuleFromSchema<TValue>(
*/
export function useFieldValue<TValue>(
initialValue: MaybeRef<TValue | undefined>,
modelValue: MaybeRef<TValue | undefined> | undefined,
path: MaybeRef<string>,
form?: PrivateFormContext
): WritableRef<TValue> {
// if no form is associated, use a regular ref.
if (!form) {
return ref(unref(initialValue)) as WritableRef<TValue>;
return ref(unref(modelValue || initialValue)) as WritableRef<TValue>;
}

// 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<TValue>({
get() {
Expand Down

0 comments on commit a11cd25

Please sign in to comment.