PeakUI

FieldError

A validation message for a form field, rendered only while the field is invalid.

Usage

import {FieldError} from "@peakui/react";

FieldError belongs inside a field such as TextField. It renders only while the field is invalid, and the field links it to the control with aria-describedby. Outside a field it renders nothing.

Examples

Native Validation

A bare <FieldError /> shows the field's own messages, which for a built-in constraint are the browser's, in the browser's language and wording. To write a message of your own for each kind of error, use the children function and read validationDetails, as in the next example.

Without children, FieldError shows the field's own validation messages: the browser's messages for constraints such as isRequired and type="email", and the strings returned from the field's validate function. Submit the form empty to see it.

Render Function

Per-error messages need the children function: <FieldError>{({validationDetails}) => ...}</FieldError>. validationDetails is the control's native ValidityState, so valueMissing, tooShort, typeMismatch and the rest say which constraint failed. A static child such as <FieldError>Invalid</FieldError> replaces every message with that one text.

Customization

Tailwind CSS

<FieldError className="text-xs font-medium">Enter a valid email address.</FieldError>

Global CSS

@layer components {
  .field-error {
    @apply text-xs;
  }
}

Styling Reference

CSS Classes

The FieldError component uses this CSS class (view source styles):

  • .field-error - Base error message styles

Interactive States

  • Visible: [data-visible] - present whenever the message is rendered

Slots

  • [data-slot="field-error"]

API Reference

FieldError

PropTypeDefaultDescription
childrenReact.ReactNode | (values: FieldErrorRenderProps) => React.ReactNode-The message, or a function of the validation state. Defaults to the field's validation messages
classNamestring | (values: FieldErrorRenderProps) => string-Additional classes, or a function of the validation state
elementTypestring'span'The element to render. Use 'div' for block-level children
renderDOMRenderFunction<'div', FieldErrorRenderProps>-Replaces the rendered element with your own

Render Props

When children or className is a function, it receives:

PropTypeDescription
isInvalidbooleanWhether the field is invalid
validationErrorsstring[]The current error messages
validationDetailsValidityStateThe native validity state of the control

On this page