Handle Submission
Submission is an essential part of any form building.
Formulaik was built to make form submission seamless and easy.
Once all the validations have passed, the onSubmit callback is called with the values provided by the user and a few extra options:
Illustration​
const onSubmit = async (values) => {
const { email, password } = values
try {
await myapi.submit({ email, password })
}
catch(e) {
throw (new Error('Could not sign in: ', e.message))
}
return { message: t("Email validated") }
}
When onSubmit is called a few things happen under the hood:
- The form is in the
isSubmittingstate. This means all your components are disabled, and if yoursubmitcomponent allows it, it will be in a loading state. You have nothing to manage here. - When everything goes well, you can return a JSON object that contain the
messagefield to notify the user the operation succeeded. - In case of an error, simply throw the error. Formulaik will catch it and notify the user accordingly.
That's it!