Skip to content

Commit ed20891

Browse files
committed
fix: trigger validation with setFieldValue by default closes #4314
1 parent b71fe6e commit ed20891

File tree

4 files changed

+17
-13
lines changed

4 files changed

+17
-13
lines changed

.changeset/spicy-berries-boil.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: trigger validation with setFieldValue by default closes #4314

packages/vee-validate/src/index.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ export {
3535
InputBindsConfig,
3636
LazyComponentBindsConfig,
3737
LazyInputBindsConfig,
38-
SetFieldValueOptions,
3938
} from './types';
4039
export { useResetForm } from './useResetForm';
4140
export { useIsFieldDirty } from './useIsFieldDirty';

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,15 +161,8 @@ export interface FormState<TValues> {
161161
export type FormErrors<TValues extends GenericObject> = Partial<Record<Path<TValues>, string | undefined>>;
162162
export type FormErrorBag<TValues extends GenericObject> = Partial<Record<Path<TValues>, string[]>>;
163163

164-
export interface SetFieldValueOptions {
165-
force: boolean;
166-
}
167164
export interface FormActions<TValues extends GenericObject, TOutput = TValues> {
168-
setFieldValue<T extends Path<TValues>>(
169-
field: T,
170-
value: PathValue<TValues, T>,
171-
opts?: Partial<SetFieldValueOptions>
172-
): void;
165+
setFieldValue<T extends Path<TValues>>(field: T, value: PathValue<TValues, T>, shouldValidate?: boolean): void;
173166
setFieldError(field: Path<TValues>, message: string | string[] | undefined): void;
174167
setErrors(fields: FormErrors<TValues>): void;
175168
setValues(fields: PartialDeep<TValues>): void;

packages/vee-validate/src/useForm.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,11 @@ export function useForm<
569569
/**
570570
* Sets a single field value
571571
*/
572-
function setFieldValue<T extends Path<TValues>>(field: T | PathState, value: PathValue<TValues, T> | undefined) {
572+
function setFieldValue<T extends Path<TValues>>(
573+
field: T | PathState,
574+
value: PathValue<TValues, T> | undefined,
575+
shouldValidate = true
576+
) {
573577
const clonedValue = deepCopy(value);
574578
const path = typeof field === 'string' ? field : (field.path as Path<TValues>);
575579
const pathState = findPathState(path);
@@ -578,6 +582,9 @@ export function useForm<
578582
}
579583

580584
setInPath(formValues, path, clonedValue);
585+
if (shouldValidate) {
586+
validateField(path);
587+
}
581588
}
582589

583590
/**
@@ -599,7 +606,7 @@ export function useForm<
599606
},
600607
set(value) {
601608
const pathValue = unref(path);
602-
setFieldValue(pathValue, value);
609+
setFieldValue(pathValue, value, false);
603610
pathState.validated = true;
604611
pathState.pending = true;
605612
validateField(pathValue).then(() => {
@@ -644,7 +651,7 @@ export function useForm<
644651
const newValue = state && 'value' in state ? state.value : getFromPath(initialValues.value, field);
645652

646653
setFieldInitialValue(field, deepCopy(newValue));
647-
setFieldValue(field, newValue as PathValue<TValues, typeof field>);
654+
setFieldValue(field, newValue as PathValue<TValues, typeof field>, false);
648655
setFieldTouched(field, state?.touched ?? false);
649656
setFieldError(field, state?.errors || []);
650657
}
@@ -660,7 +667,7 @@ export function useForm<
660667
state.validated = false;
661668
state.touched = resetState?.touched?.[state.path as Path<TValues>] || false;
662669

663-
setFieldValue(state.path as Path<TValues>, getFromPath(newValues, state.path));
670+
setFieldValue(state.path as Path<TValues>, getFromPath(newValues, state.path), false);
664671
setFieldError(state.path as Path<TValues>, undefined);
665672
});
666673

0 commit comments

Comments
 (0)