Form submission
Handle submit, transform values, async submission and server errors
Package
onSubmit handler
form.onSubmit creates the onSubmit handler for the form element. It calls event.preventDefault(),
validates the form and then calls one of two callbacks:
handleSubmit– called with form values when validation passeshandleValidationFailure– optional, called with validation errors when validation fails
Use handleValidationFailure to perform an action when the user tries to submit a form
with invalid values, for example, to show a notification:
Focus the first invalid field
form.focusInput('path') focuses an input or the first enabled control inside a group.
Use it in handleValidationFailure to focus the first invalid field. form.getInputNode('path')
still returns the element with data-path for DOM access. In a custom control, put
data-form-focus on its preferred focusable child if it is not the first input:
Submitting state
form.submitting is true while the promise returned from handleSubmit is pending.
It is set back to false when the promise resolves or rejects. Use it to disable inputs
and show a loading state on the submit button:
You can also set form.submitting manually with form.setSubmitting:
Server errors
To display errors returned from the backend, call form.setErrors or form.setFieldError
in handleSubmit. Errors are cleared when the user changes the field value. See
Form errors for all error handlers:
transformValues
Use transformValues to transform values before they get submitted in the onSubmit handler.
For example, it can be used to merge several fields into one or to convert types:
Get transformed values
form.getTransformedValues returns transformed values outside of form.onSubmit.
Pass values to transform as an argument, by default it transforms form.getValues():
To type a submit handler declared outside of form.onSubmit, use the
TransformedValues type.
onSubmitPreventDefault option
onSubmitPreventDefault option controls when form.onSubmit calls event.preventDefault():
always(default) - always callevent.preventDefault()never- never callevent.preventDefault()validation-failed- callevent.preventDefault()only if validation failed
onReset handler
form.onReset is the onReset handler for the form element, it calls form.reset.
Use it with a type="reset" button to reset the form: