Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/full-taxis-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"formik": patch
---

Reset isSubmitting when validation or sync onSubmit throws
5 changes: 5 additions & 0 deletions .changeset/stuck-issubmitting-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'formik': patch
---

Reset `isSubmitting` and `isValidating` when validation throws an error, and reset `isSubmitting` when `onSubmit` throws synchronously. Before this, the form was stuck in a submitting state forever.
32 changes: 26 additions & 6 deletions packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -334,13 +334,21 @@ export function useFormik<Values extends FormikValues = FormikValues>({
const validateFormWithHighPriority = useEventCallback(
(values: Values = state.values) => {
dispatch({ type: 'SET_ISVALIDATING', payload: true });
return runAllValidations(values).then(combinedErrors => {
if (!!isMounted.current) {
dispatch({ type: 'SET_ISVALIDATING', payload: false });
dispatch({ type: 'SET_ERRORS', payload: combinedErrors });
return runAllValidations(values).then(
combinedErrors => {
if (!!isMounted.current) {
dispatch({ type: 'SET_ISVALIDATING', payload: false });
dispatch({ type: 'SET_ERRORS', payload: combinedErrors });
}
return combinedErrors;
},
actualException => {
if (!!isMounted.current) {
dispatch({ type: 'SET_ISVALIDATING', payload: false });
}
throw actualException;
}
return combinedErrors;
});
);
}
);

Expand Down Expand Up @@ -773,6 +781,11 @@ export function useFormik<Values extends FormikValues = FormikValues>({
return;
}
} catch (error) {
// onSubmit threw before it could call setSubmitting(false),
// so we have to clean up here
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_FAILURE' });
}
throw error;
}

Expand Down Expand Up @@ -800,6 +813,13 @@ export function useFormik<Values extends FormikValues = FormikValues>({
}
}
return;
},
error => {
// validation itself failed, don't leave isSubmitting stuck on true
if (!!isMounted.current) {
dispatch({ type: 'SUBMIT_FAILURE' });
}
throw error;
}
);
});
Expand Down
27 changes: 27 additions & 0 deletions packages/formik/test/Formik.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,19 @@ describe('<Formik>', () => {
});
});

it('should reset isSubmitting and isValidating if the validate function throws an error', async () => {
const err = new Error('Async Error');
const validate = jest.fn().mockRejectedValue(err);
const { getProps } = renderFormik({ validate });

await act(async () => {
await expect(getProps().submitForm()).rejects.toThrow('Async Error');
});

expect(getProps().isSubmitting).toBe(false);
expect(getProps().isValidating).toBe(false);
});

describe('submitForm helper should not break promise chain if handleSubmit has returned rejected Promise', () => {
it('submitForm helper should not break promise chain if handleSubmit has returned rejected Promise', async () => {
const error = new Error('This Error is typeof Error');
Expand All @@ -559,6 +572,20 @@ describe('<Formik>', () => {
});
});
});

it('should reset isSubmitting if onSubmit throws synchronously', async () => {
const error = new Error('This Error is thrown synchronously');
const onSubmit = () => {
throw error;
};
const { getProps } = renderFormik({ onSubmit });

await act(async () => {
await expect(getProps().submitForm()).rejects.toEqual(error);
});

expect(getProps().isSubmitting).toBe(false);
});
});

describe('with validate (ASYNC)', () => {
Expand Down