Submitting a form
When defining a form, the onSubmit
function is expected to return a Promise<Result>
,
where Result
can either be Success
or Failure
.
tsx
import {failure ,success ,useFormo } from "@buildo/formo";Âconstlogin = (username : string,password : string) => {if (username === "admin" &&password === "password") {returnsuccess (undefined );} else {returnfailure ("wrong username/password combination!");}};Âexport constMyForm = () => {const {fieldProps ,handleSubmit ,isSubmitting ,formErrors } =useFormo ({initialValues : {username : "",password : "",},fieldValidators : () => ({}),},{onSubmit : async (values ) =>login (values .username ,values .password ),});Âreturn (<div ><TextField label ="username" {...fieldProps ("username")} /><TextField label ="password" {...fieldProps ("password")} />Â<button onClick ={handleSubmit }disabled ={isSubmitting }>Login</button >Â{formErrors }</div >);};
tsx
import {failure ,success ,useFormo } from "@buildo/formo";Âconstlogin = (username : string,password : string) => {if (username === "admin" &&password === "password") {returnsuccess (undefined );} else {returnfailure ("wrong username/password combination!");}};Âexport constMyForm = () => {const {fieldProps ,handleSubmit ,isSubmitting ,formErrors } =useFormo ({initialValues : {username : "",password : "",},fieldValidators : () => ({}),},{onSubmit : async (values ) =>login (values .username ,values .password ),});Âreturn (<div ><TextField label ="username" {...fieldProps ("username")} /><TextField label ="password" {...fieldProps ("password")} />Â<button onClick ={handleSubmit }disabled ={isSubmitting }>Login</button >Â{formErrors }</div >);};
In order to perform the form submission you can run handleSubmit
(usually by
passing it to a <button>
of some sort).
When the handleSubmit
is run, a few things happen:
isSubmitting
becomestrue
until thePromise
completes (either successfully or not)all fields
isTouched
are marked astrue
and all field validations are run. If any of these validations fails,onSubmit
is not run.if the
Promise
resolves to aFailure
,formErrors
will contain the error returned. This allows you - for instance - to display form-level errors originated during the form submittion.if the
Promise
resolves to aSuccess
, the form submission has succeeded andformErrors
will beundefined
.