Forms and User Input
Build forms that validate clearly, submit reliably and stay accessible, using React Hook Form with shared Zod schemas.
Intermediate · about 6 hours · difficulty 3/5
Learn first: State, Effects and Hooks, Runtime Validation with Zod
Forms are where users give your app data: signups, job applications, payment details. They must be fast, clear and forgiving.
Controlled vs uncontrolled
Controlled inputs keep every keystroke in React state; uncontrolled inputs let the DOM hold the value and read it on submit. For larger forms, React Hook Form uses uncontrolled inputs under the hood, which keeps re-renders low.
Validation that helps
- Plug your Zod schema into React Hook Form with a resolver, so client and server share rules.
- Show errors next to the field, in text, after the user leaves the field or submits.
- Disable the submit button while submitting, and show server errors (like "email already registered") clearly.
Submission
Send JSON with fetch or call a server action. Handle success (redirect or toast), validation errors (map them back to fields) and network failures (keep the user's input; never wipe the form on error).
Special inputs
File uploads, date pickers, phone numbers with country codes, and multi-step wizards each have quirks. Prefer native inputs where possible for accessibility and mobile keyboards.
Pitfalls
- Validating on every keystroke and showing red errors before the user has finished typing.
- Losing all input when a request fails.
- Double submissions creating duplicate records.
You are ready to move on when you can build a multi-step internship application form with shared validation, clear errors and safe submission.
Key concepts
- Controlled vs uncontrolled inputs
- React Hook Form
- Zod resolver and shared schemas
- Error display and timing
- Submit states and double submits
- File inputs
- Multi-step forms
Mini project
Build a three-step internship application (personal details, education with CGPA, resume link and cover note) with React Hook Form and Zod, a review step, and a mock submit that sometimes fails so you handle errors.
Check yourself
How do you prevent a user who double-clicks Submit from creating two applications, on both the client and the server?
Free resources
- React Hook Form (React Hook Form)
- Reacting to Input with State (React)
- Learn Forms (web.dev)