Forms, Validation and FormData
Build, explain and debug React 19 interfaces: components, rendering, state, effects, identity, forms and actions, server components, concurrency, performance and testing, ending with architecture and typing work on real component challenges.
What you will be able to do
#- Read submitted values from FormData by control name.
- Validate inside a form action and render the returned errors.
- Combine client and server validation and expose errors accessibly.
Overview
#FormData is the native contract
A form submission produces a FormData object that maps each named control to its value. React 19 lets a form take a function as its action, and that function receives the FormData. This keeps the form working without a controlled input per field.
function Signup() {
async function action(formData: FormData) {
const email = String(formData.get('email') ?? '');
// validate and submit
}
return (
<form action={action}>
<input name="email" type="email" />
<button>Sign up</button>
</form>
);
}
Every control that should be submitted needs a name attribute. Checkboxes and multi-selects yield multiple values, so read them with getAll.
Validation and useActionState
useActionState wraps an action so it can return a result and expose a pending flag. Return validation errors from the action instead of throwing, and render them next to the fields.
const [error, formAction, isPending] = useActionState(async (_prev, formData: FormData) => {
const email = String(formData.get('email') ?? '');
if (!email.includes('@')) return 'Enter a valid email';
return null;
}, null);
Client and server validation
Validate on the client for fast feedback and on the server for trust. Never rely on client validation alone: the request can be crafted. Use the same rules on both sides where possible.
Pitfall: stale values after submit
After a successful submit, reset the fields so the next entry starts clean. Do not leave stale values in the DOM. If you use controlled inputs, reset the state that backs them.
Accessible errors
Associate each error with its field with aria-describedby, and put the error text in a live region so screen readers announce it. Mark the field with aria-invalid when it fails.
Exercises
#Reference answers and expected output are authored, not executed in this browser. Nothing you type is run, and there is no Run or Pass control on this page.
Exercise 1Predict
What does this action receive when the checkbox is unchecked?
Read the code, then choose the best answer. The reference explanation stays hidden until you respond. stimulus.tsx<form action={action}> <input type="checkbox" name="subscribe" /> <button>Save</button> </form>Exercise 2Implement
Implement a form action that validates a required name and returns an error string, wired with useActionState.
starter.tsxexport function NameForm() { return null; }Reveal the reference answer
reference.tsximport { useActionState } from 'react'; async function action(_prev: string | null, formData: FormData) { const name = String(formData.get('name') ?? '').trim(); if (!name) return 'Name is required'; return null; } export function NameForm() { const [error, formAction, isPending] = useActionState(action, null); return ( <form action={formAction}> <label htmlFor="name">Name</label> <input id="name" name="name" aria-invalid={error ? true : undefined} /> {error && <p role="alert">{error}</p>} <button disabled={isPending}>{isPending ? 'Saving' : 'Save'}</button> </form> ); }Authored expected output: Submitting an empty name shows the error and keeps the form usable; a valid name clears it.
The action returns an error string instead of throwing, so useActionState exposes it as the first tuple value. The pending flag disables the button while the action runs.
Exercise 3Debug
This form never submits the field. Explain and fix.
starter.tsx<form action={action}> <input id="email" type="email" /> <button>Submit</button> </form>Reveal the reference answer
reference.tsx<form action={action}> <input id="email" name="email" type="email" /> <button>Submit</button> </form>Authored expected output: formData.get('email') now returns the typed value.
FormData is keyed by the control's name attribute, not its id. Without a name the control is not included in the submission, so the action sees nothing.
Exercise 4Test
Describe a test that proves an invalid email shows an error and a valid one does not.
starter.tsxrender(<Signup />)Reveal the reference answer
reference.tsxType an invalid value, submit, and assert the error text is in the document and the field has aria-invalid. Then type a valid value, submit, and assert the error is gone. Drive the form through the submit event so the action runs.Authored expected output: Assertions: error present with aria-invalid after an invalid submit; error absent after a valid submit.
Testing through submission exercises the action, the returned result and the rendering of the error together, which is the behavior users depend on.
Common misconceptions
#- The action prop only works on the server. Client components can pass a function action and receive FormData.
- FormData reads only text inputs. It includes checkboxes, selects, files and repeated names, which you read with getAll.
- Client validation is enough. Requests can bypass the client, so validate on the server too.
- Errors should be thrown. Return validation results from the action so the form can render them.
Follow-up practice
#- react-use-optimistic
- react-actions-and-server-functions
Your progress
#Answers, drafts and completion marks are stored only in this browser. Nothing is sent to a server and no account is used.
Verification: authored. Answers and outputs on this page are authored reference material, not executed results.