Skip to content

Commit 68080d2

Browse files
committedJul 8, 2023
feat: use silent validation when field is initialized closes #4312
1 parent e354a13 commit 68080d2

File tree

3 files changed

+21
-6
lines changed

3 files changed

+21
-6
lines changed
 

Diff for: ‎.changeset/tough-pans-drum.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': patch
3+
---
4+
5+
feat: use silent validation when field is initialized closes #4312

Diff for: ‎packages/vee-validate/src/types/forms.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ export interface PrivateFormContext<TValues extends GenericObject = GenericObjec
228228
keepValuesOnUnmount: MaybeRef<boolean>;
229229
validateSchema?: (mode: SchemaValidationMode) => Promise<FormValidationResult<TValues, TOutput>>;
230230
validate(opts?: Partial<ValidationOptions>): Promise<FormValidationResult<TValues, TOutput>>;
231-
validateField(field: Path<TValues>): Promise<ValidationResult>;
231+
validateField(field: Path<TValues>, opts?: Partial<ValidationOptions>): Promise<ValidationResult>;
232232
stageInitialValue(path: string, value: unknown, updateOriginal?: boolean): void;
233233
unsetInitialValue(path: string): void;
234234
handleSubmit: HandleSubmitFactory<TValues, TOutput> & { withControlled: HandleSubmitFactory<TValues, TOutput> };

Diff for: ‎packages/vee-validate/src/useForm.ts

+15-5
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,13 @@ export function useForm<
149149
return;
150150
}
151151

152+
// Move the error from the extras path if exists
153+
if (typeof field === 'string') {
154+
if (extraErrorsBag.value[normalizeFormPath(field) as Path<TValues>]) {
155+
delete extraErrorsBag.value[normalizeFormPath(field) as Path<TValues>];
156+
}
157+
}
158+
152159
state.errors = normalizeErrorItem(message);
153160
state.valid = !state.errors.length;
154161
}
@@ -291,10 +298,9 @@ export function useForm<
291298

292299
pathStates.value.push(state);
293300

294-
// if it has errors before, validate it.
295301
if (errors.value[pathValue] && !initialErrors[pathValue]) {
296302
nextTick(() => {
297-
validateField(pathValue);
303+
validateField(pathValue, { mode: 'silent' });
298304
});
299305
}
300306

@@ -500,6 +506,10 @@ export function useForm<
500506
return;
501507
}
502508

509+
nextTick(() => {
510+
validateField(path, { mode: 'silent' });
511+
});
512+
503513
if (pathState.multiple && pathState.fieldsCount) {
504514
pathState.fieldsCount--;
505515
}
@@ -738,20 +748,20 @@ export function useForm<
738748
};
739749
}
740750

741-
async function validateField(path: Path<TValues>): Promise<ValidationResult> {
751+
async function validateField(path: Path<TValues>, opts?: Partial<ValidationOptions>): Promise<ValidationResult> {
742752
const state = findPathState(path);
743753
if (state) {
744754
state.validated = true;
745755
}
746756

747757
if (schema) {
748-
const { results }: FormValidationResult<TValues, TOutput> = await validateSchema('validated-only');
758+
const { results }: FormValidationResult<TValues, TOutput> = await validateSchema(opts?.mode || 'validated-only');
749759

750760
return results[path] || { errors: [], valid: true };
751761
}
752762

753763
if (state?.validate) {
754-
return state.validate();
764+
return state.validate(opts);
755765
}
756766

757767
if (!state) {

0 commit comments

Comments
 (0)
Please sign in to comment.