Skip to content

Commit daf589b

Browse files
authored
feat(core): rename onError to onInvalid (#9)
* feat(core): support `onInvalid` to instead of `onError` * docs: update dcoument * docs: update dcoument * test: add test for `onInvalid()`
1 parent 8585450 commit daf589b

File tree

3 files changed

+92
-1
lines changed

3 files changed

+92
-1
lines changed

docs/api/use-form.md

+4
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,10 @@ This is your form submission handler, witch will pass your form's `values`. But
230230
If `onSubmit()` function is synchronous, you need to call `setSubmitting(false)` yourself.
231231
:::
232232

233+
### onInvalid
234+
235+
This is your invalid handler, called when the form is submitted with invalid values.
236+
233237
## Returns
234238

235239
### values

packages/core/src/composables/useForm.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,9 @@ export interface UseFormOptions<Values extends FormValues> {
5757
reValidateMode?: ValidateMode;
5858
validateOnMounted?: boolean;
5959
onSubmit: (values: Values, helper: FormSubmitHelper) => void | Promise<any>;
60+
onInvalid?: (errors: FormErrors<Values>) => void;
6061
/**
61-
* @deprecated Will be removed in a major release. Please use `watch(errors, callback)` instead.
62+
* @deprecated Will be removed in a major release. Please use `onInvalid()` instead.
6263
*/
6364
onError?: (errors: FormErrors<Values>) => void;
6465
validate?: (values: Values) => void | object | Promise<FormErrors<Values>>;
@@ -199,6 +200,7 @@ export function useForm<Values extends FormValues = FormValues>(
199200
reValidateMode = 'change',
200201
onSubmit,
201202
onError,
203+
onInvalid,
202204
} = options;
203205

204206
let initialValues = deepClone(options.initialValues);
@@ -511,6 +513,7 @@ export function useForm<Values extends FormValues = FormValues>(
511513
});
512514
} else {
513515
dispatch({ type: ACTION_TYPE.SUBMIT_FAILURE });
516+
onInvalid?.(errors);
514517
onError?.(errors);
515518
}
516519
});

packages/core/tests/composables/useForm.test.ts

+84
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,8 @@ describe('useForm', () => {
785785
});
786786

787787
await wrapper.find('button[type="submit"]').trigger('click');
788+
await sleep();
789+
788790
expect(onSubmit).toHaveBeenCalledTimes(0);
789791
});
790792

@@ -949,6 +951,88 @@ describe('useForm', () => {
949951
expect(wrapper.find('span').text()).toBe('false');
950952
});
951953

954+
it('when onInvalid with validation pass', async () => {
955+
const onInvalid = vi.fn();
956+
957+
const Comp = defineComponent({
958+
setup() {
959+
const { handleSubmit } = useForm({
960+
initialValues: {
961+
name: '',
962+
},
963+
validate() {
964+
return undefined;
965+
},
966+
onSubmit: noop,
967+
onInvalid,
968+
});
969+
970+
return {
971+
handleSubmit,
972+
};
973+
},
974+
975+
template: `
976+
<form @submit="handleSubmit">
977+
<button type="submit">Submit</button>
978+
</form>
979+
`,
980+
});
981+
982+
document.body.innerHTML = `<div id="app"></div>`;
983+
const wrapper = mount(Comp, {
984+
attachTo: '#app',
985+
});
986+
987+
await wrapper.find('button[type="submit"]').trigger('click');
988+
await sleep();
989+
990+
expect(onInvalid).toHaveBeenCalledTimes(0);
991+
});
992+
993+
it('when onInvalid with validation error', async () => {
994+
const onInvalid = vi.fn();
995+
const errors = {
996+
name: 'name is required',
997+
};
998+
999+
const Comp = defineComponent({
1000+
setup() {
1001+
const { handleSubmit } = useForm({
1002+
initialValues: {
1003+
name: '',
1004+
},
1005+
validate() {
1006+
return errors;
1007+
},
1008+
onSubmit: noop,
1009+
onInvalid,
1010+
});
1011+
1012+
return {
1013+
handleSubmit,
1014+
};
1015+
},
1016+
1017+
template: `
1018+
<form @submit="handleSubmit">
1019+
<button type="submit">Submit</button>
1020+
</form>
1021+
`,
1022+
});
1023+
1024+
document.body.innerHTML = `<div id="app"></div>`;
1025+
const wrapper = mount(Comp, {
1026+
attachTo: '#app',
1027+
});
1028+
1029+
await wrapper.find('button[type="submit"]').trigger('click');
1030+
await sleep();
1031+
1032+
expect(onInvalid).toHaveBeenCalledTimes(1);
1033+
expect(onInvalid).toHaveBeenCalledWith(errors);
1034+
});
1035+
9521036
it('when invoke handleReset', async () => {
9531037
const Comp = defineComponent({
9541038
setup() {

0 commit comments

Comments
 (0)