Agent skill
form-patterns
Use when building forms in React or Next.js. Covers React Hook Form with Zod validation, multi-step wizards, server actions, file uploads, and accessible form UX.
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/other/form-patterns
SKILL.md
Form Patterns
Stack Decision
| Need | Use |
|---|---|
| Client-side React forms | React Hook Form + Zod |
| Next.js server mutations | Server Actions + useActionState |
| Simple 1-2 field forms | Uncontrolled + native validation |
| Complex multi-step | React Hook Form + state machine |
React Hook Form + Zod
Use zodResolver(schema) with useForm. Register inputs with {...register('field')}. Display errors.field.message with role='alert' and aria-describedby.
Multi-Step Wizard
Track current step in state. Merge step data on each submit. Each step is its own RHF instance with defaultValues from accumulated data.
Server Actions (Next.js)
Mark action files 'use server'. Parse FormData with Zod's safeParse. Return { errors } on failure. Client uses useActionState(action, initialState) returning [state, formAction, isPending].
File Upload
Use useRef for hidden file input. Handle drag-and-drop via onDrop/onDragOver. Transfer dropped files to input via new DataTransfer() + dt.items.add(file) for form submission.
Validation UX
Use mode: 'onBlur' for inline validation. Debounce async validators (username availability). Prefer inline-on-blur for most fields, on-submit for short forms.
Accessibility Checklist
- Every
<input>has a<label>with matchinghtmlFor/id - Error messages use
role="alert"and link viaaria-describedby - Required fields use
aria-required="true"(not justrequiredattribute) - Focus the first error field on submit failure
- Disabled submit buttons still explain why (tooltip or adjacent text)
Gotchas
- Controlled vs uncontrolled: React Hook Form is uncontrolled by default;
Controllerwraps controlled components (Select, DatePicker). Don't mixregisterwithvalueprop - FormData with checkboxes: unchecked checkboxes are absent from FormData, not
false-- parse withformData.get('field') === 'on' - File input reset: setting
input.value = ''is the only way to clear; React state won't do it - Server Action errors:
useActionStatereplacesuseFormStatein React 19; always return structured error objects, not thrown errors - Zod
.transform(): transforms run after validation;z.coerce.number()for FormData string-to-number
Cross-References
- frontend:react-state-management -- managing form state alongside global state
- frontend:nextjs-app-router-patterns -- server actions, revalidation, and progressive enhancement
- languages:pydantic-and-data-validation -- server-side schema validation mirroring Zod patterns
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?