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.
tsximport {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 >);};
tsximport {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:
isSubmittingbecomestrueuntil thePromisecompletes (either successfully or not)all fields
isTouchedare marked astrueand all field validations are run. If any of these validations fails,onSubmitis not run.if the
Promiseresolves to aFailure,formErrorswill contain the error returned. This allows you - for instance - to display form-level errors originated during the form submittion.if the
Promiseresolves to aSuccess, the form submission has succeeded andformErrorswill beundefined.