Skip to content

Commit

Permalink
fix: trigger validation with setFieldValue by default closes #4314
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jun 13, 2023
1 parent b71fe6e commit ed20891
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-berries-boil.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vee-validate': patch
---

fix: trigger validation with setFieldValue by default closes #4314
1 change: 0 additions & 1 deletion packages/vee-validate/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ export {
InputBindsConfig,
LazyComponentBindsConfig,
LazyInputBindsConfig,
SetFieldValueOptions,
} from './types';
export { useResetForm } from './useResetForm';
export { useIsFieldDirty } from './useIsFieldDirty';
Expand Down
9 changes: 1 addition & 8 deletions packages/vee-validate/src/types/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,8 @@ export interface FormState<TValues> {
export type FormErrors<TValues extends GenericObject> = Partial<Record<Path<TValues>, string | undefined>>;
export type FormErrorBag<TValues extends GenericObject> = Partial<Record<Path<TValues>, string[]>>;

export interface SetFieldValueOptions {
force: boolean;
}
export interface FormActions<TValues extends GenericObject, TOutput = TValues> {
setFieldValue<T extends Path<TValues>>(
field: T,
value: PathValue<TValues, T>,
opts?: Partial<SetFieldValueOptions>
): void;
setFieldValue<T extends Path<TValues>>(field: T, value: PathValue<TValues, T>, shouldValidate?: boolean): void;
setFieldError(field: Path<TValues>, message: string | string[] | undefined): void;
setErrors(fields: FormErrors<TValues>): void;
setValues(fields: PartialDeep<TValues>): void;
Expand Down
15 changes: 11 additions & 4 deletions packages/vee-validate/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,11 @@ export function useForm<
/**
* Sets a single field value
*/
function setFieldValue<T extends Path<TValues>>(field: T | PathState, value: PathValue<TValues, T> | undefined) {
function setFieldValue<T extends Path<TValues>>(
field: T | PathState,
value: PathValue<TValues, T> | undefined,
shouldValidate = true
) {
const clonedValue = deepCopy(value);
const path = typeof field === 'string' ? field : (field.path as Path<TValues>);
const pathState = findPathState(path);
Expand All @@ -578,6 +582,9 @@ export function useForm<
}

setInPath(formValues, path, clonedValue);
if (shouldValidate) {
validateField(path);
}
}

/**
Expand All @@ -599,7 +606,7 @@ export function useForm<
},
set(value) {
const pathValue = unref(path);
setFieldValue(pathValue, value);
setFieldValue(pathValue, value, false);
pathState.validated = true;
pathState.pending = true;
validateField(pathValue).then(() => {
Expand Down Expand Up @@ -644,7 +651,7 @@ export function useForm<
const newValue = state && 'value' in state ? state.value : getFromPath(initialValues.value, field);

setFieldInitialValue(field, deepCopy(newValue));
setFieldValue(field, newValue as PathValue<TValues, typeof field>);
setFieldValue(field, newValue as PathValue<TValues, typeof field>, false);
setFieldTouched(field, state?.touched ?? false);
setFieldError(field, state?.errors || []);
}
Expand All @@ -660,7 +667,7 @@ export function useForm<
state.validated = false;
state.touched = resetState?.touched?.[state.path as Path<TValues>] || false;

setFieldValue(state.path as Path<TValues>, getFromPath(newValues, state.path));
setFieldValue(state.path as Path<TValues>, getFromPath(newValues, state.path), false);
setFieldError(state.path as Path<TValues>, undefined);
});

Expand Down

0 comments on commit ed20891

Please sign in to comment.