Submitting
Handling a Submit
handleSubmit turns a function into a submit function that validates the form, waits for its server checks, and calls the function only with valid values:
const send = form.handleSubmit(async contact => {
await sendMessage(contact)
})
const outcome = await send()
A call resolves to 'invalid' when errors remain, which then become visible, and to 'skipped' while a submit is already running, so a double click sends the values only once.
Submitting One Part
Every node has its own handleSubmit, so the Continue button of a wizard can submit only its own step:
const submitDelivery = form.group('delivery').handleSubmit(async delivery => {
await saveDelivery(delivery)
showStep('payment')
})The submit covers the node and everything below it, so errors elsewhere neither block it nor become visible, and an inactive part resolves to 'skipped'.
Showing Errors from the Server
The server may reject values that passed every check in the browser. Its errors are written with setErrors on the submitted node, with paths relative to that node:
const signUp = form.handleSubmit(async account => {
const response = await createAccount(account)
if (response.errors) {
form.setErrors(
response.errors.map(error => ({
path: error.field,
code: error.message,
})),
)
}
})The call resolves to 'rejected', and each error is shown on its field and blocks every submit that covers it until the user changes that value.
Validating on the Server
The server validates a request with the same definition, by creating a form from the data of the request and calling validate:
import { createForm } from '@formtree/core'
import { checkoutForm } from './forms/checkout'
import { listShippingCountries, placeOrder } from './store'
export async function handleCheckout(request: Request, cartId: string) {
const form = createForm(checkoutForm, {
initialValue: await request.json(),
deps: { cartId, countries: await listShippingCountries() },
})
const result = await form.validate()
if (result.valid) {
await placeOrder(result.value)
return new Response(null, { status: 201 })
}
if (result.errors.length > 0) {
return Response.json({ errors: result.errors }, { status: 422 })
}
return new Response(null, { status: 503 })
}validate resolves to the output, or to the errors with paths from the root, which the submit function of the whole form passes to setErrors unchanged.
Checks on the Server
validate runs every validateAsync check at once, without waiting for a blur or a debounce, so run calls on the server whatever it calls in the browser, and the form needs its deps there as well. An inactive part is left out of the output. A check whose run throws leaves no error, only an entry in failedChecks, so that request fails as unavailable.
Focusing the First Error
After a failed submit, focus moves to the first field on the page that has an error, or to the error summary with shouldFocusError: 'summary':
const send = form.handleSubmit(
async contact => {
await sendMessage(contact)
},
{ shouldFocusError: 'summary' },
)Focus finds the element of a field by its id, so a custom input, such as a group of radio buttons, puts that id on the element that should receive focus.
Hidden Fields and Custom Focus
A field hidden on the page, such as one in a closed dialog, is skipped. To focus it instead, the dialog can be opened in onInvalid, which runs before focus moves, and focus waits for the promise it returns. focusElement determines how an element is focused, for example with smooth scrolling. Every focus option can also be set once for every form in App Setup.
Submitting from a Button Outside the Form
A button placed outside the <form> element can still submit it through the HTML form attribute:
<template>
<form id="delivery" novalidate @submit.prevent="submitDelivery">…</form>
<footer>
<button type="submit" form="delivery">Continue</button>
</footer>
</template>return (
<>
<form
id="delivery"
noValidate
onSubmit={event => {
event.preventDefault()
void submitDelivery()
}}
>
…
</form>
<footer>
<button type="submit" form="delivery">
Continue
</button>
</footer>
</>
)<form id="delivery" novalidate>…</form>
<footer>
<button type="submit" form="delivery">Continue</button>
</footer>Keeping the Button Enabled
FormTree provides no flag that disables the submit button while the form is invalid. Pressing an enabled button shows every error and focuses the first one, whereas a disabled button gives no indication of what is wrong. An edit form can disable Save until something changes, as Initial Values and Reset shows.