Forms and Validation — React Hook Form and Zod
Build forms with React Hook Form, validate with shared Zod schemas on client and server, and show helpful errors.
Collect Valid Data Without Making Users Guess
A form is a contract between a person and your application. A learner might create a study session with a title, subject, and due date. The interface should explain what information is needed, show exactly which value needs attention, and preserve the user's work when a correction is required. A red border with no explanation is not helpful validation.
React Hook Form manages field state and submission with limited re-rendering. Zod describes the shape and rules of data in executable TypeScript. Together they let a client display quick feedback while the server independently checks untrusted input before using it. We will build a small session form; use the app's existing versions of these packages rather than copying an install command with guessed version numbers.
| Layer | What it can do | What it cannot guarantee |
|---|---|---|
| Browser validation | Give immediate feedback and prevent common mistakes. | It can be bypassed by a custom client or direct request. |
| Shared schema | Keep the expected data shape and rules in one place. | It does not prove that a user may perform an action. |
| Server validation | Reject malformed input at the trust boundary. | It does not replace authorization or database constraints. |
| Database constraints | Protect stored data against invalid relationships or duplicates. | They cannot explain every correction in the UI. |
Use a Schema to Describe Accepted Input
Start from the data the server is willing to accept, not from whatever fields happen to be on screen. A schema can trim surrounding whitespace, enforce a useful title length, and constrain a date string to the expected representation. Infer the TypeScript input type from that schema so the interface and the validation contract do not drift independently.
Validation answers whether a value has an acceptable shape. Business rules such as "this session belongs to the signed-in student" require authenticated context and a database check; the request body cannot authorize itself by including a student ID.
import { z } from "zod"
export const createSessionSchema = z.object({
title: z.string().trim().min(3, "Use at least 3 characters").max(100, "Keep the title under 100 characters"),
subject: z.string().trim().min(1, "Choose a subject").max(60, "Keep the subject under 60 characters"),
dueDate: z.string().regex(/^\d{4}-\d{2}-\d{2}$/, "Use a valid date"),
})
export type CreateSessionInput = z.infer<typeof createSessionSchema>The regular expression checks a date's text format, not whether that calendar date exists. If impossible dates must be rejected, add a dedicated calendar-date check. Make the rule match the product's needs and use the same schema on the server.
Connect Fields and Surface Validation Messages
Register each input with React Hook Form, pass a Zod resolver, and use the form state for errors and submission status. Keep visible labels associated with controls. Field-level messages should explain the correction and be programmatically connected to the input so assistive technology can discover them.
"use client"
import { useForm } from "react-hook-form"
import { zodResolver } from "@hookform/resolvers/zod"
import { createSessionSchema, type CreateSessionInput } from "@/lib/session-schema"
type Props = { onCreate: (input: CreateSessionInput) => Promise<void> }
export function CreateSessionForm({ onCreate }: Props) {
const { register, handleSubmit, formState: { errors, isSubmitting } } = useForm<CreateSessionInput>({
resolver: zodResolver(createSessionSchema),
defaultValues: { title: "", subject: "", dueDate: "" },
})
return (
<form onSubmit={handleSubmit(onCreate)} noValidate>
<label htmlFor="title">Session title</label>
<input id="title" {...register("title")} aria-invalid={Boolean(errors.title)}
aria-describedby={errors.title ? "title-error" : undefined} />
{errors.title && <p id="title-error" role="alert">{errors.title.message}</p>}
<label htmlFor="subject">Subject</label>
<input id="subject" {...register("subject")} aria-invalid={Boolean(errors.subject)}
aria-describedby={errors.subject ? "subject-error" : undefined} />
{errors.subject && <p id="subject-error" role="alert">{errors.subject.message}</p>}
<label htmlFor="dueDate">Due date</label>
<input id="dueDate" type="date" {...register("dueDate")} aria-invalid={Boolean(errors.dueDate)}
aria-describedby={errors.dueDate ? "dueDate-error" : undefined} />
{errors.dueDate && <p id="dueDate-error" role="alert">{errors.dueDate.message}</p>}
<button type="submit" disabled={isSubmitting}>
{isSubmitting ? "Saving…" : "Create session"}
</button>
</form>
)
}The form component owns the interaction; its parent provides the create operation. Showing a pending state prevents accidental duplicate clicks, but the server must still handle duplicate requests safely if duplicate creation would be harmful.
Make Errors Specific, Recoverable, and Accessible
Validate at a sensible time. Showing a required-field error before a learner has had a chance to fill anything can feel noisy. A common pattern is to validate on submit first and update errors as the person corrects the form. Confirm the chosen resolver and validation mode with the installed library API in your project.
For a server response containing multiple field errors, map only known fields to their matching controls. Keep a general message for failures that cannot be attributed to one field. Do not render raw exception messages from a server or database as user-facing copy; they may expose internal details and are rarely actionable.
- Give each control a visible, persistent label.
- Explain a field error in plain language and identify the affected field.
- Associate the message with the control using aria-describedby and reflect invalid state.
- Keep values in place when validation fails so the learner can correct them.
- Provide a form-level message when the server cannot complete the operation.
Parse the Request Before Calling the Data Layer
A client can alter JavaScript or send an HTTP request without using your form. Parse the submitted body at the server boundary, return a safe and predictable response for invalid input, then derive the owner from the authenticated session. The following route-handler example keeps the persistence function behind a typed boundary; replace the placeholder with your app's server-only data-access code.
import { NextResponse } from "next/server"
import { createSessionSchema } from "@/lib/session-schema"
import { requireStudent } from "@/lib/auth"
import { insertSession } from "@/lib/sessions"
export async function POST(request: Request) {
const student = await requireStudent()
if (!student) {
return NextResponse.json({ error: "Sign in to create a session" }, { status: 401 })
}
let body: unknown
try {
body = await request.json()
} catch {
return NextResponse.json({ error: "Request body must be valid JSON" }, { status: 400 })
}
const parsed = createSessionSchema.safeParse(body)
if (!parsed.success) {
return NextResponse.json({ error: "Check the form fields" }, { status: 400 })
}
const session = await insertSession({ ...parsed.data, studentId: student.id })
return NextResponse.json({ session }, { status: 201 })
}The code authenticates before writing and takes ownership from the verified server-side identity, not from a submitted studentId. A production handler also needs deliberate error handling around persistence, authorization checks for updates, and any request-size or rate controls appropriate to the endpoint.
Validation Is One Part of Data Integrity
Different layers protect different invariants. A Zod schema can reject an empty title; application logic can confirm that a chosen subject belongs to the signed-in learner; a database constraint can prevent invalid references from being stored. Keep each rule at the layer that has the required information and can enforce it reliably.
| Rule | Best place to enforce | Reason |
|---|---|---|
| Title is not blank | Shared schema | The same simple constraint can guide both UI and server parsing. |
| Student can edit this session | Authenticated server logic | The server knows the verified principal and requested record. |
| Referenced student exists | Database foreign key | The database can enforce referential integrity for every writer. |
| No duplicate booking for the same slot | Domain logic plus database constraint | Concurrent requests can race, so a database invariant is important. |
For dates, money, and identifiers, be explicit about representation. An HTML date input submits a string; a currency input may use decimal text; neither should be casually treated as a floating-point amount or localised display value. Parse and validate according to the domain before persisting.
Keep the Client, Schema, and Server in Sync
Trusting client-side validation as protection
Why: Requests can be sent without using the page at all.
Fix: Parse and validate the request body on every server write.
Accepting an owner ID from the form
Why: A caller can change the ID to target another learner’s data.
Fix: Derive ownership from the authenticated server-side identity.
Showing an error with no correction guidance
Why: The learner cannot tell what value or action is expected.
Fix: Write a concise message next to the named field and preserve entered values.
Using a schema as a substitute for authorization
Why: A value can be valid in shape but unavailable to this user.
Fix: Check permissions and resource ownership in trusted server logic.
Build a Safe Session-Creation Flow
Practice
Create, validate, and submit one study session
- Define a Zod schema for a title, subject, and due date, then infer a TypeScript input type.
- Build a labelled React Hook Form component with field-level messages and a pending submit state.
- Add a server handler that treats the JSON body as unknown, parses it, and returns a safe 400 response on invalid input.
- Require an authenticated student and assign record ownership from the server identity, never from a browser-supplied owner ID.
- Test a valid submission, a blank title, malformed JSON, and a request without a signed-in user.
Hint: Keep schema, UI, and route code in their appropriate modules. The route should import the schema but should not trust that the browser already ran it.
🎯 Key Takeaways
- ✓React Hook Form manages field interaction; Zod describes and parses the accepted data shape.
- ✓A shared schema can improve consistency, but server validation remains mandatory.
- ✓Accessible forms need visible labels, specific errors, and clear links between fields and messages.
- ✓Authentication identifies the caller; authorization decides what that caller may change.
- ✓Never trust a submitted owner ID when the server can derive ownership from verified identity.
- ✓Use application rules and database constraints for invariants that schemas alone cannot enforce.
Next
What comes next
Place data loading and mutations in the right part of a Next.js app, and connect forms to server-side behavior.
Lesson 05 · Next.js — Routing, Server Components and Forms →HELP US IMPROVE
How was this lesson?
Your note is shared with the Chaduvuko team for review. Please don’t include personal or sensitive information.
Spotted a mistake or something unclear? Suggest a fix on GitHub or email us. Select the text first and it is quoted for you.
Discussion
0Have a better approach? Found something outdated? Share it — your knowledge helps everyone learning here.
