Skip to content

Commit d4fafc9

Browse files
committed
feat: allow handleBlur to run validations optionally
1 parent 3e4a7c1 commit d4fafc9

File tree

4 files changed

+11
-12
lines changed

4 files changed

+11
-12
lines changed

.changeset/little-kings-perform.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': minor
3+
---
4+
5+
"feat: allow handleBlur to run validations"

packages/vee-validate/src/Field.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,10 @@ const FieldImpl = /** #__PURE__ */ defineComponent({
161161
resolveValidationTriggers(props);
162162

163163
function baseOnBlur(e: Event) {
164-
handleBlur(e);
164+
handleBlur(e, validateOnBlur);
165165
if (isCallable(ctx.attrs.onBlur)) {
166166
ctx.attrs.onBlur(e);
167167
}
168-
169-
if (validateOnBlur) {
170-
validateField();
171-
}
172168
}
173169

174170
function baseOnInput(e: Event | unknown) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ export interface PrivateFieldContext<TValue = unknown> {
137137
handleReset(): void;
138138
validate: FieldValidator;
139139
handleChange(e: Event | unknown, shouldValidate?: boolean): void;
140-
handleBlur(e?: Event): void;
140+
handleBlur(e?: Event, shouldValidate?: boolean): void;
141141
setState(state: Partial<FieldState<TValue>>): void;
142142
setTouched(isTouched: boolean): void;
143143
setErrors(message: string | string[]): void;

packages/vee-validate/src/useField.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,8 +159,11 @@ function _useField<TValue = unknown>(
159159
/**
160160
* Handles common onBlur meta update
161161
*/
162-
const handleBlur = () => {
162+
const handleBlur = (evt?: unknown, shouldValidate = false) => {
163163
meta.touched = true;
164+
if (shouldValidate) {
165+
validateWithStateMutation();
166+
}
164167
};
165168

166169
async function validateCurrentValue(mode: SchemaValidationMode) {
@@ -259,11 +262,6 @@ function _useField<TValue = unknown>(
259262

260263
function setValue(newValue: TValue, shouldValidate = true) {
261264
value.value = newValue;
262-
if (!shouldValidate) {
263-
validateValidStateOnly();
264-
return;
265-
}
266-
267265
const validateFn = shouldValidate ? validateWithStateMutation : validateValidStateOnly;
268266
validateFn();
269267
}

0 commit comments

Comments
 (0)