Forms
govuk-react’s form controls render native input, textarea and select elements, so forms work as HTML forms do: they submit without JavaScript, and form libraries can read and set their values.
Each Field passes its other props, including ref, name, value and event handlers, to the control, not the form group. Put the error message in errorMessage, and the field links it with aria-describedby.
govuk-react’s tests run each of these libraries against every form control, including the reveals, character count, password input, file upload and search autocomplete.
React Hook Form
Spread register onto the field. Its ref reaches the native element:
import { useForm } from 'react-hook-form';
import { Button, InputField, Radios } from 'govuk-react';
function Example() {
const { register, handleSubmit, formState: { errors } } = useForm();
const where = { required: 'Select where you live' };
return (
<form onSubmit={handleSubmit(console.log)} noValidate>
<InputField
label="Full name"
{...register('name', { required: 'Enter your full name' })}
errorMessage={errors.name?.message}
/>
<Radios.Field idPrefix="where" legend="Where do you live?" errorMessage={errors.where?.message}>
<Radios.ItemComposite id="where" value="england" label="England" {...register('where', where)} />
<Radios.ItemComposite id="where-wales" value="wales" label="Wales" {...register('where', where)} />
</Radios.Field>
<Button>Continue</Button>
</form>
);
}
Pass the same rules to each checkbox or radio in a group, because a later register without rules replaces them.
Conform
Conform’s getInputProps and the others return a key, which React won’t take in a spread, so take it out first. A checkbox or radio part sets its own type, so take that out too:
import { getFormProps, getInputProps, useForm } from '@conform-to/react';
import { parseWithZod } from '@conform-to/zod/v4';
import { z } from 'zod';
import { InputField } from 'govuk-react';
const schema = z.object({ name: z.string({ error: 'Enter your full name' }) });
const control = ({ key, ...props }) => props;
const choice = ({ key, type, ...props }) => props;
function Example({ lastResult }) {
const [form, fields] = useForm({
lastResult,
onValidate: ({ formData }) => parseWithZod(formData, { schema }),
});
return (
<form {...getFormProps(form)}>
<InputField
label="Full name"
{...control(getInputProps(fields.name, { type: 'text' }))}
errorMessage={fields.name.errors?.[0]}
/>
</form>
);
}
Validate on the server too, and pass its result as lastResult, so the form submits and shows its errors without JavaScript.
TanStack Form
Control each field with value and onChange:
import { useForm } from '@tanstack/react-form';
import { InputField } from 'govuk-react';
function Example() {
const form = useForm({ defaultValues: { name: '' }, onSubmit: ({ value }) => console.log(value) });
return (
<form
noValidate
onSubmit={(event) => {
event.preventDefault();
form.handleSubmit();
}}
>
<form.Field name="name">
{(field) => (
<InputField
label="Full name"
name={field.name}
value={field.state.value}
onBlur={field.handleBlur}
onChange={(event) => field.handleChange(event.target.value)}
/>
)}
</form.Field>
</form>
);
}
Errors
Follow the Design System’s guidance on validation: show an error summary at the top of the page, linked to each field with an error, and put “Error: “ at the start of the page title.