Submit
#
When does form submission run?The form is submitted when:
<Form />
exposed byuseForm()
is used and it receives a form submit event.form.submit()
is called.form.handleSubmit(event)
is called passing a form submit event.
#
What happens during form submission?When a form is submitted:
- All the fields in the form values are touched.
- Form validation runs.
- If there are errors: those errors are set, submission stops, and
onFailedSubmit
callback is called. - Otherwise
onSubmit
callback is called with the validated values of the form. - If
onSubmit
returns form errors, those errors are set to the form.
#
ExampleLet's see an example:
When we press the submit button, the form submission starts.
In case we have not filled in the name field:
onValidate
returns a form error- this error is set to the form
- submission stops
onFailedSubmit
is called
Instead, if we have filled in the name field, onSubmit
is called and:
- if our API returns some api errors:
- these errors are converted to a FormError shape, and returned by
onSubmit
- these converted errors are set to the form
- the user sees these errors in the form
- these errors are converted to a FormError shape, and returned by
- otherwise everyone is happy, and this form made its job!