Skip to content

Commit

Permalink
Allow validation options to be overridden for submit
Browse files Browse the repository at this point in the history
  • Loading branch information
rzane committed Feb 25, 2022
1 parent 8e299af commit 68bc74f
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Create a submit handler for the form.

- `form` **[Form](#form)<Value, Result>**
- `fn` **SubmitFn<Result>**
- `opts` **ValidateOptions** (optional, default `{touch:true}`)

#### Examples

Expand Down
7 changes: 4 additions & 3 deletions src/useSubmit.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { FormEvent } from "react";
import { Form, Submit } from "./types";
import { Form, Submit, ValidateOptions } from "./types";
import { toPromise } from "./utilities/toPromise";
import { useMounted } from "./utilities/useMounted";
import { useEventCallback } from "./utilities/useEventCallback";
Expand All @@ -20,7 +20,8 @@ type SubmitFn<T> = (values: T) => void | Promise<any>;
*/
export function useSubmit<Value, Result>(
form: Form<Value, Result>,
fn: SubmitFn<Result>
fn: SubmitFn<Result>,
opts: ValidateOptions = { touch: true }
): Submit {
const isMounted = useMounted();

Expand All @@ -36,7 +37,7 @@ export function useSubmit<Value, Result>(

form.setSubmitting(true);

return toPromise(() => form.validate({ touch: true }))
return toPromise(() => form.validate(opts))
.then(result => {
if (!result.valid) return;

Expand Down

0 comments on commit 68bc74f

Please sign in to comment.