# FRM-002a Show a field's error as text at the field

Rule. Verdict: Unanimous (2 of 2). Status: decided. https://blackwoodblake.com/design-system/patterns/frm-002a/

## Rule

When a field or a group of inputs has an invalid value, show the error as visible text at that field, and never only in a banner, a dialog or a tooltip somewhere else.

## Why

People correct a value where the value is. If the message lives in another place, they have to remember it, scroll between the two, or guess which field it means, and the message may be gone by the time they start typing. Placing it at the field and tying it to the control in code means sighted and screen reader users meet the problem and the field together. [WCAG 2.2, 3.3.1 Error Identification](https://www.w3.org/TR/WCAG22/#error-identification) asks that the field in error is named and the problem is described in text, but it leaves the place open (see [Understanding 3.3.1](https://www.w3.org/WAI/WCAG22/Understanding/error-identification.html)). This rule is our design choice for that place.

## Do

- A field labelled "Postcode" holds "1234". Right at the field, the text "Enter a five-digit postcode, for example 11359." appears with an error icon, and stays there while the person edits.
- A bank account entered as a branch code box and an account number box gets one message for the pair. If only the branch code is wrong, the message says so: "The branch code must be four or five digits".
- The server sends the form back because the email address is already registered. The message "This email address already has an account. Sign in or use a different address." is shown at the email field, not only at the top.
- You add a summary at the top of a long form as an extra. Each line in it links to its field and uses the same words as the message at that field.
- The control points to its message with `aria-describedby` and is marked with `aria-invalid`, so the message is read out with the field. Justified's `Field` component does this for you.
- An end date comes before a start date. Show the message at the end date, since that is the field people most often change. This is our own choice for errors that involve two fields.

## Don't

- Show only a banner that says "The form contains errors" and leave the person to find the fields.
- Put the message behind an icon that reveals it on hover or focus.
- Open a dialog with the error that must be closed before the person can fix the value.
- Mark the field with a red outline and nothing else. Always add text, and an icon if you like.
- Place the text visually next to the field but leave it unlinked in code, so a screen reader reads the field without its error.

## Exceptions

- Problems that are not about a value the person entered, such as a service that is down, missing permission or not being eligible, do not belong at a field. Show them as a message for the page or the form instead.
- This rule does not set whether the message goes above or below the input, or whether it replaces the hint text. Both are left to the component. If the message does take the place of the hint, it must repeat whatever the hint said that the person needs to fix the value.
- When the error appears is covered by FRM-002b.
- What a free-text field says when its length limit is reached or passed is the guideline FRM-017c. An error for text that is too long still shows at the field, as this rule asks.

## How to check

Checked by judgement: someone has to weigh what the code does. Each item is a sign the rule may be broken; code that follows the rule never matches.

- Field errors sent only to `toast.error()`, an `<Alert>` at the top or a `Dialog`, with no message at the field, such as no `<FormMessage />` or `Field` `error`. Problems that belong to no field, such as a service that is down, are fine there (see Exceptions).
- A field marked in error only by colour, such as `aria-invalid:border-destructive` or `border-red-500`, with no error text.
- An error message shown only in a `Tooltip` or `HoverCard` on an icon, instead of as text at the field.
- A control with `aria-invalid="true"` whose `aria-describedby` is missing or does not include the `id` of its visible error message.
- When the server rejects a value, such as an email address already in use, does the message show at that field and not only at the top?

---

Rule texts © Carl Magnus Swahn and Justified contributors, licensed under CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Generated from 79 rules and 23 guidelines.
