Skip to content

Showing Errors

Validation errors generally come from three layers. Wherever you read them, errors are always an array of objects with a message property.

Creating reactive UI

A field rerenders automatically, so you can read its errors directly:

tsx
import { useForm } from '@tanstack/react-form'
import type { AnyFieldApi } from '@tanstack/react-form'

interface ErrorsProps {
  field: AnyFieldApi
}

function Errors(props: ErrorsProps) {
  return props.field.errors.map((error, i) => <p key={i}>{error.message}</p>)
}

function Component() {
  const form = useForm({
    defaultValues: { firstName: '', guests: [] },
    validators: [/* ... */],
  })

  return (
    <>
      <form.Field name="firstName">
        {(field) => (
          // field rerenders when errors change
          <Errors field={field} />
        )}
      </form.Field>
      {/* Remember: Array fields need to opt in to meta changes */}
      <form.ArrayField name="guests" subscribeToMeta>
        {(field) => <Errors field={field} />}
      </form.ArrayField>
    </>
  )
}

For more information on form.ArrayField, see the Array Fields guide.

Forms and form groups do not rerender on their own. To show their errors, you need to subscribe to their error state. See the reactivity guide for more information:

tsx
import { useForm } from '@tanstack/react-form'

function Component() {
  const form = useForm({
    defaultValues: {
      firstName: '',
      group: { nested: '' },
    },
    validators: [/* ... */],
  })

  return (
    <>
      <form.Subscribe selector={(state) => state.errors}>
        {(errors) => errors.map((error, i) => <p key={i}>{error.message}</p>)}
      </form.Subscribe>
      <form.FormGroup
        name="group"
        // ... additional props
      >
        {(group) => (
          <group.Subscribe selector={(state) => state.errors}>
            {(errors) =>
              errors.map((error, i) => <p key={i}>{error.message}</p>)
            }
          </group.Subscribe>
        )}
      </form.FormGroup>
    </>
  )
}

Unique error messages

TanStack Form does not remove duplicate error messages. Some schema structures might route multiple issues to the same field with the same message.

Choose when field errors appear

When a form validator runs, it usually creates errors for multiple fields at once. That might cause a field to become invalid even though the user has never interacted with it.

We can hide errors while a field is untouched, but this requires repeating the same check:

tsx
<form.Field name="firstName">
  {(field) => {
    // We have to do this for every relevant UI component,
    // for every field of every form...
    const isInvalid = field.meta.isTouched && field.meta.isInvalid

    // This property lost its meaning.
    return field.meta.isInvalid
  }}
</form.Field>

Instead, we can define the desired behavior once for the form using errorVisibility:

ts
const form = useForm({
  defaultValues: { firstName: '' },
  validators: [/* ... */],
  errorVisibility: ({ fieldState }) => fieldState.meta.isTouched,
})

With this policy, an untouched field keeps isInvalid false and errors as an empty array. Once the field is touched, these properties reflect any errors:

tsx
<form.Field name="firstName">
  {(field) => {
    // The errorVisibility can now replace this
    // const isInvalid = field.meta.isTouched && field.meta.isInvalid

    return field.meta.isInvalid
  }}
</form.Field>

Important

Error visibility filters what the field shows. It does not clear or reset any errors associated with the field.

Override visibility for a field

If a field needs a different visibility policy, you can provide an override for that field:

tsx
<form.Field
  name="firstName"
  errorVisibility={({ fieldState }) => fieldState.meta.isBlurred}
>
  {(field) => {
    return field.meta.isInvalid
  }}
</form.Field>

Read hidden errors

The error visibility filter applies to properties such as field.meta.isValid and field.errors. To bypass the filter for a direct read, you can use the original property:

tsx
<form.Field
  name="firstName"
  errorVisibility={({ fieldState }) => fieldState.meta.isBlurred}
>
  {(field) => {
    // If the field isn't blurred, this is what each property would show:
    field.meta.isInvalid // false
    field.errors // []

    field.meta.original.isInvalid // true if errors are present
    field.meta.original.errors // the array of errors

    return null
  }}
</form.Field>

Reuse a visibility policy

The visibility filter only needs to be declared once per form, but you still need to define it for each form that uses it. To keep the logic consistent across forms, you can create a reusable callback:

ts
import { createErrorVisibility, useForm } from '@tanstack/react-form'

const showErrorsAfterBlurOrSubmit = createErrorVisibility(
  ({ fieldState, state }) =>
    fieldState.meta.isBlurred || state.submissionAttempts > 0,
)

function ProfileForm() {
  const form = useForm({
    defaultValues: { firstName: '' },
    validators: [/* ... */],
    errorVisibility: showErrorsAfterBlurOrSubmit,
  })

  // ...
}