Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add submission failed handler #3937

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions docs/api/formik.md
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,17 @@ included in the `FormikBag`.

> IMPORTANT: If `onSubmit` is async, then Formik will automatically set `isSubmitting` to `false` on your behalf once it has resolved. This means you do NOT need to call `formikBag.setSubmitting(false)` manually. However, if your `onSubmit` function is synchronous, then you need to call `setSubmitting(false)` on your own.

### `onSubmitFailed: (errors: object | Promise<FormikErrors<Values>>, formikBag: FormikBag) => void`

Your form submission failed handler. It is passed your forms `errors` and the
"FormikBag", which includes an object containing a subset of the
[injected props and methods](#formik-render-methods-and-props) (i.e. all the methods
with names that start with `set<Thing>` + `resetForm`) and any props that were
passed to the wrapped component.

Note: `errors`, `touched`, `status` and all event handlers are NOT
included in the `FormikBag`.

### `validate?: (values: Values) => FormikErrors<Values> | Promise<any>`

_Note: I suggest using `validationSchema` and Yup for validation. However,
Expand Down
8 changes: 8 additions & 0 deletions packages/formik/src/Formik.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,15 @@ export function useFormik<Values extends FormikValues = FormikValues>({
isInitialValid,
enableReinitialize = false,
onSubmit,
onSubmitFailed,
...rest
}: FormikConfig<Values>) {
const props = {
validateOnChange,
validateOnBlur,
validateOnMount,
onSubmit,
onSubmitFailed,
...rest,
};
const initialValues = React.useRef(props.initialValues);
Expand Down Expand Up @@ -794,6 +796,8 @@ export function useFormik<Values extends FormikValues = FormikValues>({
// throw combinedErrors;
if (isInstanceOfError) {
throw combinedErrors;
} else {
executeSubmitFailed()
}
}
return;
Expand Down Expand Up @@ -859,6 +863,10 @@ export function useFormik<Values extends FormikValues = FormikValues>({
return onSubmit(state.values, imperativeMethods);
});

const executeSubmitFailed = useEventCallback(() => {
return onSubmitFailed?.(state.errors, imperativeMethods);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you swap this to stateRef.current.errors? state can be stale sometimes

});

const handleReset = useEventCallback(e => {
if (e && e.preventDefault && isFunction(e.preventDefault)) {
e.preventDefault();
Expand Down
9 changes: 9 additions & 0 deletions packages/formik/src/types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,15 @@ export interface FormikConfig<Values> extends FormikSharedConfig {
values: Values,
formikHelpers: FormikHelpers<Values>
) => void | Promise<any>;

/**
* Submission failed handler(invoke when validation fails)
*/
onSubmitFailed?: (
errors: object | Promise<FormikErrors<Values>>,
formikHelpers: FormikHelpers<Values>
) => void;

/**
* A Yup Schema or a function that returns a Yup schema
*/
Expand Down