Form errors

form.errors is an object with one message per field. Its keys are the field paths, such as email or user.email. A field without an error has no key.

Set errors from the server

Validation in the browser cannot know everything. When the server rejects a value, call setFieldError or setErrors with the message. The field shows it right away, and it clears when the user changes that field.

General

The server rejects this email, so the field shows its message.

Clear errors

clearFieldError(path) removes one error and clearErrors() removes all of them. Changing a field also clears its error, unless validateInputOnChange is on for it.

Handle errors on submit

onSubmit takes a second handler that runs when validation fails. It receives the errors and the values.

<form onSubmit={form.onSubmit(save, (errors) => console.log(errors))}>

APIs

Errors

NameDescriptionType
errorsone message per field pathRecord<string, string>
setErrorsreplace all errors(errors: Record<string, string>) => void
setFieldErrorset the error of one field(path: string, message: string) => void
clearErrorsremove all errors() => void
clearFieldErrorremove the error of one field(path: string) => void
Edit this page on GitHub