Functional Weave
Code in Python

react.form.validated-input

A text, email or number input that checks itself with form.validate-input and shows the error accessibly.

React ^19 · TypeScript only A React component: use it from a TypeScript project with React installed. A client component ("use client").

1.0.0 · published 2026-10-10 by charlie · Anterra

Pinned by 14 tests, run in TypeScript.

What it does

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.

"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} />

For example

  1. a required text input: required on the input, no error before showError

    <ValidatedInput
      id="name"
      label="Full name"
      rules={{"label":"Full name","kind":"text","required":true,"minLength":null,"maxLength":null,"pattern":null,"patternMessage":null,"min":null,"max":null}}
    />

    renders

    <div class="fune-field"><label class="fune-label" for="name">Full name</label><input class="fune-input" id="name" type="text" required="" name="name"/></div>
  2. an email: type email, spell check off, autocomplete

    <ValidatedInput
      id="email"
      label="Email address"
      autoComplete="email"
      rules={{"label":"Email address","kind":"email","required":true,"minLength":null,"maxLength":null,"pattern":null,"patternMessage":null,"min":null,"max":null}}
    />

    renders

    <div class="fune-field"><label class="fune-label" for="email">Email address</label><input class="fune-input" id="email" type="email" autoComplete="email" spellCheck="false" required="" name="email"/></div>

The function

Written for React ^19, in TypeScript only. A component: it takes its props and renders HTML, which the tests below pin exactly.

A React capability: React ^19 · TypeScript only. This capability has no Python implementation, so it is shown in TypeScript. A Python project cannot use it: fune build stops and names where it was required. Your choice of Python is kept for every other page.

export function ValidatedInput(props: ValidatedInputProps): JSX.Element

Its props, ValidatedInputProps. A ? marks one the caller may leave out.

labelReactNodethe question, or the field's name
rulesInputRuleswhat the value must be; rules.label names the field in the messages
id?stringthe input's id; React's useId() when left out
name?stringwhat the value is submitted as; the id when left out
hint?ReactNodehelp under the label
showError?booleancheck the current value and show the message: after the field is touched or the form submitted
error?ReactNodean error to show instead, such as the server's answer; marks the input invalid
value?stringthe value, for a controlled input (with onChange)
defaultValue?stringthe starting value, for an uncontrolled one
onChange?(value: string) => voidcalled with the new value as the person types
onBlur?() => voidcalled when the input loses focus, to mark the field touched
autoComplete?stringthe autocomplete token, such as email, name or postal-code (WCAG 1.3.5)
inputMode?stringthe keyboard to show; numeric for integer and decimal for number when left out
spellCheck?booleanfalse for names, emails and codes
disabled?booleanshown but not editable, and not submitted
className?stringadded to the wrapper's classes
rendersJSX.Element

The type it declares, generated into your project

/** One input whose rules are data: the browser gets them as attributes, the message comes from validateInput. */
export interface ValidatedInputProps {
  /** the question, or the field's name */
  readonly label: ReactNode;
  /** what the value must be; rules.label names the field in the messages */
  readonly rules: InputRules;
  /** the input's id; React's useId() when left out */
  readonly id?: string;
  /** what the value is submitted as; the id when left out */
  readonly name?: string;
  /** help under the label */
  readonly hint?: ReactNode;
  /** check the current value and show the message: after the field is touched or the form submitted */
  readonly showError?: boolean;
  /** an error to show instead, such as the server's answer; marks the input invalid */
  readonly error?: ReactNode;
  /** the value, for a controlled input (with onChange) */
  readonly value?: string;
  /** the starting value, for an uncontrolled one */
  readonly defaultValue?: string;
  /** called with the new value as the person types */
  readonly onChange?: (value: string) => void;
  /** called when the input loses focus, to mark the field touched */
  readonly onBlur?: () => void;
  /** the autocomplete token, such as email, name or postal-code (WCAG 1.3.5) */
  readonly autoComplete?: string;
  /** the keyboard to show; numeric for integer and decimal for number when left out */
  readonly inputMode?: string;
  /** false for names, emails and codes */
  readonly spellCheck?: boolean;
  /** shown but not editable, and not submitted */
  readonly disabled?: boolean;
  /** added to the wrapper's classes */
  readonly className?: string;
}

Your code names it in one line, in the file that uses it

import { ValidatedInput } from "#fune/react.form.validated-input@^1";
impl/typescript.tsx · 61 lines · open · raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

"use client";

import { useId, useState, type JSX, type ReactNode } from "react";
import { validateInput } from "./form_validate_input.ts";  ← from form.validate-input ^1.0.0 · built alongside by fune
import { FormField, fieldIds } from "./react_form_form_field.ts";  ← from react.form.form-field ^1.0.0 · built alongside by fune
import type { ValidatedInputProps } from "./react_form_validated_input_types.ts";

function present(node: ReactNode): boolean {
  return node !== null && node !== undefined && node !== false && node !== "";
}

/**
 * A GOV.UK text input whose rules are data. The same InputRules go to the
 * browser as attributes (required, minLength, maxLength, pattern) and to
 * validateInput for the message, so the server can check a submission with
 * the very same rules in TypeScript, Python or Rust. A number is a text input
 * with a numeric keyboard, not type="number", which GOV.UK advises against;
 * its min and max are checked by validateInput, since a text input has no
 * min or max attribute.
 */
export function ValidatedInput(props: ValidatedInputProps): JSX.Element {
  const auto = useId();
  const { label, rules, hint, showError, error, value, defaultValue, onChange, onBlur, autoComplete, spellCheck, disabled, className } = props;
  const id = props.id ?? auto;
  // The text as it stands, so an uncontrolled input can still be checked.
  const [own, setOwn] = useState<string>(defaultValue ?? "");
  const current = value ?? own;
  // The rules are checked on every render, so a bad rule fails loudly even
  // before the field is touched.
  const check = validateInput(current, rules);
  const shown: ReactNode = present(error) ? error : showError && !check.valid ? check.message : null;
  const ids = fieldIds(id, present(hint), present(shown));
  const inputMode = props.inputMode ?? (rules.kind === "integer" ? "numeric" : rules.kind === "number" ? "decimal" : undefined);
  return (
    <FormField id={id} label={label} hint={hint} error={shown} className={className}>
      <input
        className={ids.error ? "fune-input fune-input--error" : "fune-input"}
        id={ids.control}
        name={props.name ?? id}
        type={rules.kind === "email" ? "email" : "text"}
        value={value}
        defaultValue={value === undefined ? defaultValue : undefined}
        autoComplete={autoComplete}
        inputMode={inputMode as JSX.IntrinsicElements["input"]["inputMode"]}
        spellCheck={spellCheck ?? (rules.kind === "email" ? false : undefined)}
        required={rules.required || undefined}
        minLength={rules.minLength ?? undefined}
        maxLength={rules.maxLength ?? undefined}
        pattern={rules.pattern ?? undefined}
        disabled={disabled}
        aria-describedby={ids.describedBy ?? undefined}
        aria-invalid={ids.error ? true : undefined}
        onChange={(event) => {
          setOwn(event.target.value);
          onChange?.(event.target.value);
        }}
        onBlur={onBlur ? () => onBlur() : undefined}
      />
    </FormField>
  );
}

Install

npm install react react-dom
fune build

With that line in your source, in a TypeScript project (language typescript in fune.project) with React ^19 installed (Functional Weave does not ship it, and fune build stops with the npm install line if it is missing), fune build resolves it and its 2 dependencies, pins them in fune.lock, downloads only the TypeScript package of each, and builds the code above into your project’s .fune/build, one readable file per capability with a header linking back here. Or pin a range in fune.project and build in one step:

fune add react.form.validated-input
Download for TypeScript react.form.validated-input-1.0.0-typescript.fune · 23,034 bytes sha256 f705b4f9ed21d951e960e80ee28d4d0cd5df855a7cb76dffff1f6594bb0e5007

The manifest, vectors and README with only the TypeScript implementation. Install it without the registry with fune add ./react.form.validated-input-1.0.0-typescript.fune, or fetch it from a terminal with fune pull react.form.validated-input@1.0.0:typescript.

It is React ^19 · TypeScript only, so there is no package for Python or Rust. The full package is one file too: react.form.validated-input-1.0.0.fune, 23,006 bytes, sha256 99d75f58e2648871e763f8b308a7ae801b08debaed41988b93ac6562d30d652c.

Customise it in your app

The seams this capability offers. Put a marker directly above a function of your own and fune build wires it into the built code; the package on the registry is not changed, the built file’s header lists it under CUSTOMISED, and fune hooks lists every hook in the project. How hooks work.

before — your function gets the arguments and returns them, changed or not, or throws to refuse the call.

// fune: before react.form.validated-input

after — your function gets the result and the arguments, and returns the final result.

// fune: after react.form.validated-input

replace — inside this capability’s code only, calls to a dependency go to your function, with the same signature. Other capabilities that use it are unaffected; write in * to replace it everywhere.

// fune: replace form.validate-input in react.form.validated-input
// fune: replace react.form.form-field in react.form.validated-input

step — your function runs at a numbered point inside the function’s body, receives the in-scope values it names as parameters, and may return replacements. List the points with fune show react.form.validated-input --steps.

// fune: step react.form.validated-input after <n|label>

Tests

A version published now needs at least 8 tests for every function, and one that expects the error for each function that throws; the registry refuses it otherwise. fune verify --all runs each case in TypeScript alone, with the React in tooling/react, and a project runs them again with fune verify. This page lists the cases; it does not run them. The exact JSON is vectors.json. A component’s tests are render tests: the props go in, and the HTML renderToStaticMarkup makes of them has to match exactly; a React warning during the render fails the test.

  1. a required text input: required on the input, no error before showError

    <ValidatedInput
      id="name"
      label="Full name"
      rules={{"label":"Full name","kind":"text","required":true,"minLength":null,"maxLength":null,"pattern":null,"patternMessage":null,"min":null,"max":null}}
    />

    renders

    <div class="fune-field"><label class="fune-label" for="name">Full name</label><input class="fune-input" id="name" type="text" required="" name="name"/></div>
  2. an email: type email, spell check off, autocomplete

    <ValidatedInput
      id="email"
      label="Email address"
      autoComplete="email"
      rules={{"label":"Email address","kind":"email","required":true,"minLength":null,"maxLength":null,"pattern":null,"patternMessage":null,"min":null,"max":null}}
    />

    renders

    <div class="fune-field"><label class="fune-label" for="email">Email address</label><input class="fune-input" id="email" type="email" autoComplete="email" spellCheck="false" required="" name="email"/></div>
  3. an invalid email with showError: GOV.UK's message, invalid and described

    <ValidatedInput
      id="email"
      label="Email address"
      showError
      value="name@"
      rules={{"label":"Email address","kind":"email","required":true,"minLength":null,"maxLength":null,"pattern":null,"patternMessage":null,"min":null,"max":null}}
    />

    renders

    <div class="fune-field fune-field--error"><label class="fune-label" for="email">Email address</label><p class="fune-error-message" id="email-error"><span class="fune-visually-hidden">Error:</span> Enter an email address in the correct format, like name@example.com</p><input class="fune-input fune-input--error" id="email" type="email" spellCheck="false" required="" aria-describedby="email-error" aria-invalid="true" name="email" value="name@"/></div>
  4. lengths go on the input as minLength and maxLength

    <ValidatedInput
      id="code"
      label="Reference"
      rules={{"label":"Reference","kind":"text","required":false,"minLength":6,"maxLength":10,"pattern":null,"patternMessage":null,"min":null,"max":null}}
    />

    renders

    <div class="fune-field"><label class="fune-label" for="code">Reference</label><input class="fune-input" id="code" type="text" minLength="6" maxLength="10" name="code"/></div>
  5. too long with showError: the length message

    <ValidatedInput
      id="code"
      label="Reference"
      showError
      value="ABCDEFGHIJK"
      rules={{"label":"Reference","kind":"text","required":false,"minLength":6,"maxLength":10,"pattern":null,"patternMessage":null,"min":null,"max":null}}
    />

    renders

    <div class="fune-field fune-field--error"><label class="fune-label" for="code">Reference</label><p class="fune-error-message" id="code-error"><span class="fune-visually-hidden">Error:</span> Reference must be 10 characters or less</p><input class="fune-input fune-input--error" id="code" type="text" minLength="6" maxLength="10" aria-describedby="code-error" aria-invalid="true" name="code" value="ABCDEFGHIJK"/></div>
  6. a pattern goes on the input; its own message when it does not match

    <ValidatedInput
      id="mobile"
      label="UK mobile number"
      showError
      value="0123"
      rules={{"label":"UK mobile number","kind":"text","required":false,"minLength":null,"maxLength":null,"pattern":"07[0-9]{9}","patternMessage":"Enter a UK mobile number, like 07700900982","min":null,"max":null}}
    />

    renders

    <div class="fune-field fune-field--error"><label class="fune-label" for="mobile">UK mobile number</label><p class="fune-error-message" id="mobile-error"><span class="fune-visually-hidden">Error:</span> Enter a UK mobile number, like 07700900982</p><input class="fune-input fune-input--error" id="mobile" type="text" pattern="07[0-9]{9}" aria-describedby="mobile-error" aria-invalid="true" name="mobile" value="0123"/></div>
  7. a whole number: numeric keyboard, below min with showError

    <ValidatedInput
      id="children"
      label="How many children do you have?"
      showError
      value="-1"
      rules={{"label":"Number of children","kind":"integer","required":true,"minLength":null,"maxLength":null,"pattern":null,"patternMessage":null,"min":"0","max":"20"}}
    />

    renders

    <div class="fune-field fune-field--error"><label class="fune-label" for="children">How many children do you have?</label><p class="fune-error-message" id="children-error"><span class="fune-visually-hidden">Error:</span> Number of children must be between 0 and 20</p><input class="fune-input fune-input--error" id="children" type="text" inputMode="numeric" required="" aria-describedby="children-error" aria-invalid="true" name="children" value="-1"/></div>
  8. a decimal: decimal keyboard, not a number with showError

    <ValidatedInput
      id="weight"
      label="Weight, in kilograms"
      showError
      value="12,5"
      rules={{"label":"Weight","kind":"number","required":false,"minLength":null,"maxLength":null,"pattern":null,"patternMessage":null,"min":"0.5","max":"500"}}
    />

    renders

    <div class="fune-field fune-field--error"><label class="fune-label" for="weight">Weight, in kilograms</label><p class="fune-error-message" id="weight-error"><span class="fune-visually-hidden">Error:</span> Weight must be a number</p><input class="fune-input fune-input--error" id="weight" type="text" inputMode="decimal" aria-describedby="weight-error" aria-invalid="true" name="weight" value="12,5"/></div>
  9. a valid value with showError shows no error; the hint still describes it

    <ValidatedInput
      id="weight"
      label="Weight, in kilograms"
      hint="For example, 72.5"
      showError
      defaultValue="72.5"
      rules={{"label":"Weight","kind":"number","required":false,"minLength":null,"maxLength":null,"pattern":null,"patternMessage":null,"min":"0.5","max":"500"}}
    />

    renders

    <div class="fune-field"><label class="fune-label" for="weight">Weight, in kilograms</label><div class="fune-hint" id="weight-hint">For example, 72.5</div><input class="fune-input" id="weight" type="text" inputMode="decimal" aria-describedby="weight-hint" name="weight" value="72.5"/></div>
  10. an error passed in wins, described after the hint

    <ValidatedInput
      id="username"
      label="Username"
      hint="Letters and numbers only"
      error="That username is taken"
      value="charles"
      rules={{"label":"Username","kind":"text","required":true,"minLength":null,"maxLength":null,"pattern":"[A-Za-z0-9]+","patternMessage":null,"min":null,"max":null}}
    />

    renders

    <div class="fune-field fune-field--error"><label class="fune-label" for="username">Username</label><div class="fune-hint" id="username-hint">Letters and numbers only</div><p class="fune-error-message" id="username-error"><span class="fune-visually-hidden">Error:</span> That username is taken</p><input class="fune-input fune-input--error" id="username" type="text" required="" pattern="[A-Za-z0-9]+" aria-describedby="username-hint username-error" aria-invalid="true" name="username" value="charles"/></div>
Show the other 4 tests
  1. empty, required and showError: Enter full name

    <ValidatedInput
      id="name"
      label="Full name"
      showError
      value="  "
      rules={{"label":"Full name","kind":"text","required":true,"minLength":null,"maxLength":null,"pattern":null,"patternMessage":null,"min":null,"max":null}}
    />

    renders

    <div class="fune-field fune-field--error"><label class="fune-label" for="name">Full name</label><p class="fune-error-message" id="name-error"><span class="fune-visually-hidden">Error:</span> Enter full name</p><input class="fune-input fune-input--error" id="name" type="text" required="" aria-describedby="name-error" aria-invalid="true" name="name" value="  "/></div>
  2. name, inputMode, spellCheck, disabled and a class of its own

    <ValidatedInput
      id="pin"
      name="pinCode"
      label="PIN"
      inputMode="numeric"
      spellCheck={false}
      disabled
      className="app-pin"
      rules={{"label":"PIN","kind":"text","required":false,"minLength":null,"maxLength":null,"pattern":"[0-9]{4}","patternMessage":null,"min":null,"max":null}}
    />

    renders

    <div class="fune-field app-pin"><label class="fune-label" for="pin">PIN</label><input class="fune-input" id="pin" type="text" inputMode="numeric" spellCheck="false" pattern="[0-9]{4}" disabled="" name="pinCode"/></div>
  3. a bad rule throws before anything is typed: min above max

    <ValidatedInput
      id="n"
      label="Number"
      rules={{"label":"Number","kind":"number","required":false,"minLength":null,"maxLength":null,"pattern":null,"patternMessage":null,"min":"10","max":"1"}}
    />

    error: min

  4. a pattern outside the subset throws

    <ValidatedInput
      id="n"
      label="Code"
      rules={{"label":"Code","kind":"text","required":false,"minLength":null,"maxLength":null,"pattern":"(?=a)a","patternMessage":null,"min":null,"max":null}}
    />

    error: unsupported pattern

Verification

Verified by the operator, not the sandbox 14 test vectors declared; none run by the registry.

Verified locally by the registry's operator, not by the sandbox. This version was published with FUNE_VERIFY_MODE=inline-dev (the operator's import of the first-party registry, or a development server): the server validated the package - its manifest, signatures, imports, safety lint and test rules - but the registry did not run its test vectors in its sandbox. The operator runs them with `fune verify --all` before importing.

Signed by the registry with key fw-9dc887227da47575 (ed25519), over its integrity and this report's digest. The full report · JSON

More from the author

# 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.

Files

PathBytes
NOTICE1,224
README.md3,380
impl/typescript.tsx2,915
vectors.json9,354