Skip to content

setError

Manually set an input error

setError:(name: string, error: FieldError, { shouldFocus?: boolean }) => void

The function allows you to manually set one or more errors.

Props

NameTypeDescription
namestring

input's name.

error{ type: string, message?: string, types: MultipleFieldErrors }

Set an error with its type and message.

config{ shouldFocus?: boolean }

Should focus the input during setting an error. This only works when the input's reference is registered, it will not work for custom register as well.

Rules

  • This method will not persist the associated input error if the input passes validation.

  • An error that is not associated with an input field will be persisted until cleared with clearErrors.

  • Can be useful in the handleSubmit method when you want to give error feedback to a user after async validation. (ex: API returns validation errors)

  • shouldFocus doesn't work when an input has been disabled.

Examples

Video

The following video explain setError API in detail.

Thank you for your support

If you find React Hook Form to be useful in your project, please consider to star and support it.

Edit