Skip to content

Commit 09d5596

Browse files
committedMay 10, 2023
fix: run validation on value change closes #4251
1 parent a210ae6 commit 09d5596

File tree

4 files changed

+49
-20
lines changed

4 files changed

+49
-20
lines changed
 

Diff for: ‎.changeset/fast-jars-protect.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'vee-validate': patch
3+
---
4+
5+
fix: run validation on value change closes #4251

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

+1-12
Original file line numberDiff line numberDiff line change
@@ -147,17 +147,6 @@ const FieldImpl = defineComponent({
147147
ctx.emit('update:modelValue', value.value);
148148
};
149149

150-
const handleInput = (e: any) => {
151-
if (!hasCheckedAttr(ctx.attrs.type)) {
152-
value.value = normalizeEventValue(e);
153-
}
154-
};
155-
156-
const onInputHandler = function handleInputWithModel(e: any) {
157-
handleInput(e);
158-
ctx.emit('update:modelValue', value.value);
159-
};
160-
161150
const fieldProps = computed(() => {
162151
const { validateOnInput, validateOnChange, validateOnBlur, validateOnModelUpdate } =
163152
resolveValidationTriggers(props);
@@ -218,7 +207,7 @@ const FieldImpl = defineComponent({
218207
validate: validateField,
219208
resetField,
220209
handleChange: onChangeHandler,
221-
handleInput: onInputHandler,
210+
handleInput: e => onChangeHandler(e, false),
222211
handleReset,
223212
handleBlur,
224213
setTouched,

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -218,11 +218,7 @@ function _useField<TValue = unknown>(
218218
// Common input/change event handler
219219
function handleChange(e: unknown, shouldValidate = true) {
220220
const newValue = normalizeEventValue(e) as TValue;
221-
222-
setValue(newValue, false);
223-
if (!validateOnValueUpdate && shouldValidate) {
224-
validateWithStateMutation();
225-
}
221+
setValue(newValue, shouldValidate);
226222
}
227223

228224
// Runs the initial validation
@@ -257,13 +253,14 @@ function _useField<TValue = unknown>(
257253
validateValidStateOnly();
258254
}
259255

260-
function setValue(newValue: TValue, validate = true) {
256+
function setValue(newValue: TValue, shouldValidate = true) {
261257
value.value = newValue;
262-
if (!validate) {
258+
if (!shouldValidate) {
259+
validateValidStateOnly();
263260
return;
264261
}
265262

266-
const validateFn = validateOnValueUpdate ? validateWithStateMutation : validateValidStateOnly;
263+
const validateFn = shouldValidate ? validateWithStateMutation : validateValidStateOnly;
267264
validateFn();
268265
}
269266

Diff for: ‎packages/vee-validate/tests/useField.spec.ts

+38
Original file line numberDiff line numberDiff line change
@@ -832,4 +832,42 @@ describe('useField()', () => {
832832
await flushPromises();
833833
expect(name?.textContent).toBe('second');
834834
});
835+
836+
test('handle change validates the field by default', async () => {
837+
let field!: FieldContext;
838+
const validator = vi.fn(val => (val ? true : REQUIRED_MESSAGE));
839+
mountWithHoc({
840+
setup() {
841+
field = useField('field', validator);
842+
},
843+
template: `<div></div>`,
844+
});
845+
846+
await flushPromises();
847+
expect(validator).toHaveBeenCalledTimes(1);
848+
expect(field.errorMessage.value).toBe(undefined);
849+
field.handleChange('');
850+
await flushPromises();
851+
expect(field.errorMessage.value).toBe(REQUIRED_MESSAGE);
852+
});
853+
854+
test('handle change can be configured to not validate the field', async () => {
855+
let field!: FieldContext;
856+
const validator = vi.fn(val => (val ? true : REQUIRED_MESSAGE));
857+
mountWithHoc({
858+
setup() {
859+
field = useField('field', validator, { validateOnValueUpdate: false });
860+
},
861+
template: `<div></div>`,
862+
});
863+
864+
await flushPromises();
865+
expect(validator).toHaveBeenCalledTimes(1);
866+
expect(field.errorMessage.value).toBe(undefined);
867+
field.handleChange('', false);
868+
await flushPromises();
869+
870+
expect(validator).toHaveBeenCalledTimes(2);
871+
expect(field.errorMessage.value).toBe(undefined);
872+
});
835873
});

0 commit comments

Comments
 (0)
Please sign in to comment.