# react.form.validated-input
A GOV.UK text input whose rules are data. One `InputRules` value (from
`form.validate-input`) says what the field needs: required, a kind (text,
email, number, integer), a length, a pattern, a min and max. The component
puts the rules the browser understands on the input as attributes, and shows
`validateInput`'s message when asked to. The server checks the submitted
value with the very same rules, in TypeScript, Python or Rust, so the browser
and the API can never disagree about what is valid.
```tsx
"use client";
import { ValidatedInput } from "#fune/react.form.validated-input@^1";
const rules = { label: "Number of children", kind: "integer", required: true,
minLength: null, maxLength: null, pattern: null, patternMessage: null, min: "0", max: "20" } as const;
<ValidatedInput id="children" label="How many children do you have?" rules={rules}
value={children} onChange={setChildren} onBlur={() => setTouched(true)} showError={touched || submitted} />
```
```python
# the API, with the same rules
from fune.form.validate_input import validate_input
check = validate_input(form["children"], rules)
if not check.valid:
errors["children"] = check.message
```
- **When the message shows.** `showError` turns the check on: set it once the
field has been touched or the form submitted (`form.state` tracks both).
Until then nothing is shown, as GOV.UK advises: do not tell people off
while they are still typing.
- **`error` wins.** An error passed in (the server's answer, such as "That
username is taken") is shown instead of the computed one.
- **Accessibility.** The label's `for`, `aria-describedby` (hint, then
error) and `aria-invalid="true"` only while an error is shown, through
`react.form.form-field`. The error message starts with a visually hidden
"Error:".
- **Attributes.** `required`, `minLength`, `maxLength` and `pattern` are on
the input, so the browser's own checks and assistive technology know them.
Put `noValidate` on the `<form>` to show these messages rather than the
browser's bubbles (GOV.UK does). `email` is `type="email"` with spell check
off. A number is `type="text"` with `inputMode` `decimal` (or `numeric` for
a whole number), not `type="number"`, which GOV.UK advises against; a text
input has no `min` or `max` attribute, so those are `validateInput`'s alone.
- **Uncontrolled works too.** With only `defaultValue` the component keeps
the text it last saw, so `showError` still checks what is in the box.
- **Bad rules throw** (min above max, a pattern outside the supported
subset): that is a bug in the page, and it fails the render rather than
quietly accepting anything.
It is a client component (it listens for input, and keeps the text in state).
Classes: those of `react.form.form-field`, plus `fune-input` and
`fune-input--error`.
Sources: GOV.UK Design System, "Text input" and "Error message"
https://design-system.service.gov.uk/components/text-input/ ,
https://design-system.service.gov.uk/components/error-message/ ; HTML,
constraint validation https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constraints .
## Notices
Portions derived from GOV.UK Frontend (https://github.com/alphagov/govuk-frontend), Copyright (c) 2017 Crown Copyright (Government Digital Service), under the MIT License; the full notice is in NOTICE.