Skip to the page

Forms, TypeScript and security · senior

Forms at scale: react-hook-form, zod and server validation

Large forms stay fast with uncontrolled inputs (react-hook-form registers inputs by ref, so typing does not re-render the form), one zod schema validates on both client and server, and the server always re-validates because client checks are only for UX.

The notes

What you must know

  • react-hook-form: register connects native inputs by ref; only fields you watch/useWatch cause re-renders. Controller wraps controlled third-party inputs.
  • One schema: zodResolver(schema) on the client; schema.safeParse(formData) in the Server Action or API; z.infer<typeof schema> gives the TypeScript type. (Zod 4 syntax, e.g. z.email({ error: "..." }), is what current Next.js docs use.)
  • Always validate on the server: the client can be bypassed with a direct request.
  • Conditional fields (GST only for businesses): useWatch scoped to the field that matters; understand shouldUnregister.
  • Async validation (PAN already registered): debounce, cancel stale checks, and re-check on the server at submit.
  • Multi-step wizard: state in one form context or URL step; drafts in sessionStorage; validate per step.
  • React 19 alternative for simple forms: <form action> + useActionState returning field errors from the server, with uncontrolled inputs.
  • Accessibility: <label> for every input, aria-invalid, aria-describedby pointing at the error text, focus the first invalid field on submit, do not rely on colour alone.

The red pen

Common traps: mistake → fix

  • Mistake: "Client validation is enough"

    Fix: anyone can POST directly.

  • Mistake: "One giant useState object for 30 fields"

    Fix: every keystroke re-renders the whole form.

  • Mistake: "Validate only on submit"

    Fix: give inline feedback after the first blur, but avoid shouting while the user types.

Easy to hard

Questions interviewers ask, easy to hard

  1. Build a 30-field KYC form (PAN, Aadhaar, address, conditional GST) that is fast and validates the same way on client and server.
  2. Why is react-hook-form faster than controlled useState per field?
  3. How do you do async validation (PAN already registered)?
  4. Multi-step wizard: where does state live and how does it survive refresh?
  5. How do you make error messages accessible?

Practise

Practise with these questions

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview