# Errors

The control's border turns red and a message under it says what to do next.

This is the rule in `dex.css` terms, for work outside the Dex codebase. Read https://brand.meetdex.ai/design.md first: it says how to set up a page and how to use the stylesheet. The page at https://brand.meetdex.ai/components/errors shows the rule on the Dex app's own React components, which only work inside the Dex codebase.

## HTML

**A field**: `aria-invalid="true"` on the control turns its edge red, and a `dex-field-error` takes the helper text's place. Point the control at the message and give the message `role="alert"`:

```html
<div class="dex-form-row">
  <label class="dex-field-label" for="email">Work email</label>
  <input class="dex-field" id="email" type="email" aria-invalid="true" aria-describedby="email-error" />
  <span class="dex-field-error" id="email-error" role="alert">Enter your work email.</span>
</div>
```

**A group**: every option takes the red edge, so the group reads as one error, with one message under it. Set `aria-invalid="true"` on each radio or checkbox (`dex-choice` turns red when the radio inside it has it):

```html
<fieldset style="border: 0; margin: 0; padding: 0; display: grid; gap: 0.75rem">
  <legend class="dex-field-label" style="margin-bottom: 0.75rem">Where do you want to work?</legend>
  <label class="dex-check-row"><input type="checkbox" class="dex-checkbox" aria-invalid="true" /> London</label>
  <label class="dex-check-row"><input type="checkbox" class="dex-checkbox" aria-invalid="true" /> Remote</label>
  <span class="dex-field-error" role="alert">Choose at least one option.</span>
</fieldset>
```

**A single checkbox**: the message lines up with the label, not the box, so indent it by the box and its gap (`padding-left: 1.875rem`).

**Form-level**: only for problems the person can't fix in a field, such as a server or network failure. Put a `dex-field-error` with `role="alert"` above the actions, and never repeat field errors there.

```html
<p class="dex-field-error" role="alert">We couldn't send your answers. Try again, or email hello@meetdex.ai.</p>
<button class="dex-button">Send application</button>
```

## States

- The edge is red at rest, on hover and on focus, where a 1px red inset doubles it. Focus never hides an error.
- The label keeps its colour. Only the control's edge and the message turn red (`--dex-destructive`).

## Writing the message

One sentence that says what to do, ending in a full stop. It starts with one of four verbs: Enter, Choose, Upload or Accept. Never "Please", "required", "invalid" or "This field".

| Situation | Pattern | Example |
| --- | --- | --- |
| Typed field left empty | Enter your {thing}. | Enter your work email. |
| Wrong format | Enter a {thing} like {example}. | Enter an email address like name@company.com. |
| Nothing chosen | Choose {a thing}. | Choose a notice period. |
| Group with nothing chosen | Choose at least one option. | Choose at least one option. |
| Out of range | Choose {a thing} {rule}. | Choose an end date after the start date. |
| File rejected | Upload {a thing} {rule}. | Upload a PDF or DOCX under 10 MB. |
| Consent not given | Accept {the thing} to continue. | Accept the terms to continue. |
| Server or network | We couldn't {action}. Try again, or {contact}. | We couldn't send your answers. Try again, or email hello@meetdex.ai. |

## Rules

- Keep the form's main button active, and show the errors when it's pressed. On submit, move focus to the first field with an error.
- A switch never errors. A slider only errors when it's required and still not set.
- For a chip group with no answer, set `aria-invalid="true"` on every chip (or on each chip's input): `dex.css` gives them all the red edge.
