Skip to content

Commit

Permalink
feat: allow handleBlur to run validations optionally
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jun 5, 2023
1 parent 3e4a7c1 commit d4fafc9
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/little-kings-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'vee-validate': minor
---

"feat: allow handleBlur to run validations"
6 changes: 1 addition & 5 deletions packages/vee-validate/src/Field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,14 +161,10 @@ const FieldImpl = /** #__PURE__ */ defineComponent({
resolveValidationTriggers(props);

function baseOnBlur(e: Event) {
handleBlur(e);
handleBlur(e, validateOnBlur);
if (isCallable(ctx.attrs.onBlur)) {
ctx.attrs.onBlur(e);
}

if (validateOnBlur) {
validateField();
}
}

function baseOnInput(e: Event | unknown) {
Expand Down
2 changes: 1 addition & 1 deletion packages/vee-validate/src/types/forms.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ export interface PrivateFieldContext<TValue = unknown> {
handleReset(): void;
validate: FieldValidator;
handleChange(e: Event | unknown, shouldValidate?: boolean): void;
handleBlur(e?: Event): void;
handleBlur(e?: Event, shouldValidate?: boolean): void;
setState(state: Partial<FieldState<TValue>>): void;
setTouched(isTouched: boolean): void;
setErrors(message: string | string[]): void;
Expand Down
10 changes: 4 additions & 6 deletions packages/vee-validate/src/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,11 @@ function _useField<TValue = unknown>(
/**
* Handles common onBlur meta update
*/
const handleBlur = () => {
const handleBlur = (evt?: unknown, shouldValidate = false) => {
meta.touched = true;
if (shouldValidate) {
validateWithStateMutation();
}
};

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

function setValue(newValue: TValue, shouldValidate = true) {
value.value = newValue;
if (!shouldValidate) {
validateValidStateOnly();
return;
}

const validateFn = shouldValidate ? validateWithStateMutation : validateValidStateOnly;
validateFn();
}
Expand Down

0 comments on commit d4fafc9

Please sign in to comment.