# Field

Label, control, hint and error message wired with aria-describedby and aria-invalid.

_Diagram: A field is one form entry in the content area: the label, the control, then a hint and an error message under it._

## Live example

### With a hint

```html
<div data-field>
  <label for="f-name">Display name</label>
  <input id="f-name" name="display-name" type="text" autocomplete="nickname" aria-describedby="f-name-hint">
  <p id="f-name-hint" data-field-hint>Shown next to your comments.</p>
</div>
```

### With a hint and an error

```html
<div data-field>
  <label for="f-email">Email</label>
  <input id="f-email" name="email" type="email" autocomplete="email" value="ada@" aria-invalid="true" aria-describedby="f-email-hint f-email-error">
  <p id="f-email-hint" data-field-hint>We only use this to reply.</p>
  <p id="f-email-error" data-field-error>Error: enter an email address like name@example.com.</p>
</div>
```

### Native validation

```html
<form action="#native-title" class="stack">
  <div data-field>
    <label for="f-code">Invite code</label>
    <input id="f-code" name="code" type="text" required minlength="6" aria-describedby="f-code-hint">
    <p id="f-code-hint" data-field-hint>Six characters or more.</p>
  </div>
  <div class="cluster">
    <button type="submit">Join</button>
  </div>
</form>
```

### A group with an error

```html
<fieldset data-field aria-describedby="f-size-error">
  <legend>Size</legend>
  <label><input type="radio" name="size" value="s"> Small</label>
  <label><input type="radio" name="size" value="m"> Medium</label>
  <p id="f-size-error" data-field-error>Error: choose a size.</p>
</fieldset>
```

## Attributes

`data-field` on the wrapper, `data-field-hint` and `data-field-error` on the messages

| Attribute or value | Where | Effect |
| --- | --- | --- |
| `data-field` | the wrapper (a `<div>`, or a `<fieldset>` for groups) |  |
| `data-field-hint` | the hint |  |
| `data-field-error` | the error message |  |

- The wrapper is a `<div>`, or a `<fieldset>` for a group of controls.
- Hide an error that does not apply with `hidden`, and remove its id from `aria-describedby`.

- **Our choice:** the secondary button sits on the page surface with a softer border (`--dui-color-border-button`, neutral step 7) and a 1px shadow. Its visible text label identifies it, so [WCAG 1.4.11](https://www.w3.org/WAI/WCAG22/Understanding/non-text-contrast.html) does not require a 3:1 boundary; fields, selects, checkboxes, radios and switches keep the 3:1 `--dui-color-border-control`.
- **Our choice:** buttons, fields, selects and badges use the 14px interface size (`--dui-font-size-ui`). Fields, selects and text areas use `--dui-font-size-field`, which is at least 16px under a coarse pointer, because iOS Safari zooms into a focused field with smaller text.
- **Our choice:** default buttons and fields are 36px tall and large ones 40px, and a coarse (touch) pointer restores 44px, between Primer and Radix (32px) and Material (40px), for comparison.
- **Our choice:** the field's error text starts with the word "Error:" in the markup, rather than a CSS icon, so every user and every screen reader gets it with no extra API. Invalid controls also get a 3px halo around them, a shape change as well as a color change.
- **Our choice:** a field is an atom here, not a molecule, because a control is not usable without its label, hint and error. Larger compositions (forms, cards, headers) are molecules and organisms for later milestones.

## Accessibility

- `<label for>` names the control ([WCAG 1.3.1](https://www.w3.org/WAI/WCAG22/Understanding/info-and-relationships.html), [3.3.2](https://www.w3.org/WAI/WCAG22/Understanding/labels-or-instructions.html)).
- Hint and error ids go in the control's [`aria-describedby`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-describedby).
- On a wrong value, set [`aria-invalid="true"`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-invalid) and show the error as text ([WCAG 3.3.1](https://www.w3.org/WAI/WCAG22/Understanding/error-identification.html)).
- Native validation gets the same look through [`:user-invalid`](https://developer.mozilla.org/en-US/docs/Web/CSS/:user-invalid), only after the user interacts.

## Specifications

- [WCAG 3.3.1](https://www.w3.org/WAI/WCAG22/Understanding/error-identification.html)
- [WCAG 3.3.2](https://www.w3.org/WAI/WCAG22/Understanding/labels-or-instructions.html)
- [WCAG 1.3.1](https://www.w3.org/WAI/WCAG22/Understanding/info-and-relationships.html)
- [MDN `aria-describedby`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-describedby)
- [MDN `aria-invalid`](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Attributes/aria-invalid)
- [MDN `:user-invalid`](https://developer.mozilla.org/en-US/docs/Web/CSS/:user-invalid)
- [ui.shadcn.com input](https://ui.shadcn.com/docs/components/input)
- [WCAG Use of color](https://www.w3.org/WAI/WCAG22/Understanding/use-of-color.html)

## See also

- [Atoms](https://domestic-ui.odonckers.dev/atoms/index.md)
- [Component API convention](https://domestic-ui.odonckers.dev/index.md#component-api)
- [Accessibility baseline](https://domestic-ui.odonckers.dev/principles/accessibility-baseline-wcag-22-aa.md)
- Source: [`css/components/atoms/field.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/atoms/field.css), [`examples/atoms/field.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/atoms/field.html)

## How other systems do it

For comparison only; these are not the standard this system follows.

- Apple Human Interface Guidelines: No equivalent
- Material Design 3: No equivalent
- Vercel Geist: No equivalent
