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
onFailedSubmitcallback is called. - Otherwise
onSubmitcallback is called with the validated values of the form. - If
onSubmitreturns form errors, those errors are set to the form.
Example#
Let's see an example:
When we press the submit button, the form submission starts.
In case we have not filled in the name field:
onValidatereturns a form error- this error is set to the form
- submission stops
onFailedSubmitis 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!