Skip to content

commandFrontend

Add Form Validation

Adds matching client and server validation to a form, with plain error messages that are announced to screen readers.

npx workspacetemplates add add-form-validation --tool claude-code

Writes 1 file to .claude/commands/

.claude/commands/add-form-validation.md15 lines · 1.2 KBFile
---
description: Adds matching client and server validation to a form, with plain error messages that are announced to screen readers.
argument-hint: "[form component or route]"
---

Add validation to the form: $ARGUMENTS

1. Find the form, its submit handler and the endpoint that receives it. Detect the validation library already in use (zod, yup, valibot, Joi, class-validator, pydantic) and use it; add nothing new unless none exists.
2. List every field with its rule: required, type, length, format, range, and cross-field rules such as matching passwords. Show this list before coding.
3. Define one schema and use it on both sides. On the server, validate before any database write, return a 422 with `{ field, message }` pairs, and never trust client checks.
4. In the UI, validate on blur and on submit, show messages beside the field, link them with `aria-describedby`, set `aria-invalid`, and move focus to the first error. Do not clear user input on failure.
5. Messages say what to do ("Enter a date after today"), not what went wrong internally.
6. Add tests for one valid case and one failure per rule.

Run the tests. Report the rules added and any rule you were unsure about, and ask before changing database constraints.

What it does

Client checks make a form pleasant, but only server checks make it safe, and the two drift apart when written separately. This command defines the rules once, applies them in the browser and on the endpoint, and keeps the messages the same.

Errors are tied to their fields and read out by assistive technology. Libraries already in the project are used instead of adding new ones.

Example

you › /add-form-validation src/routes/signup

agent › Rules: email (required, valid format), password (min 12, not equal to email), terms (must be true). One zod schema shared by SignupForm and POST /api/signup, which now returns 422 with field messages.
Focus moves to the first invalid field. 9 tests pass. Should the username be unique-checked on blur?

Questions

Why validate in two places?
The browser check gives quick feedback, while the server check is the one that protects your data. Sharing one schema keeps the two from drifting.
Will it add a validation library?
Only if the project has none. It reuses whatever you already depend on.
  • A11y Check

    CommandFrontend

    Free
  • Add Loading States

    CommandFrontend

    Free
  • Check Responsive

    CommandFrontend

    Free
  • Convert to TypeScript

    Commandtypescript

    Free