shadcn-forms
This skill should be used when the user asks about "form validation", "react-hook-form", "zod schema", "form submission", "input validation", "form errors", "useForm hook", "form field", or mentions form handling, validation, or react-hook-form integration with shadcn.
What this skill does
# shadcn/ui Forms
Build validated forms with react-hook-form, zod, and shadcn/ui components.
## Setup
```bash
npm install react-hook-form zod @hookform/resolvers
npx shadcn@latest add form input label
```
shadcn's Form component wraps react-hook-form with accessible fields:
`Form`, `FormField`, `FormItem`, `FormLabel`, `FormControl`, `FormDescription`, `FormMessage`
## Basic Form Pattern
### 1. Define Zod Schema
```tsx
import { z } from "zod"
const formSchema = z.object({
username: z.string().min(2, "Username must be at least 2 characters"),
email: z.string().email("Invalid email address"),
})
type FormValues = z.infer<typeof formSchema>
```
### 2. Create Form with useForm + FormField
```tsx
"use client"
import { zodResolver } from "@hookform/resolvers/zod"
import { useForm } from "react-hook-form"
import { Button } from "@/components/ui/button"
import {
Form, FormControl, FormField, FormItem, FormLabel, FormMessage,
} from "@/components/ui/form"
import { Input } from "@/components/ui/input"
export function MyForm() {
const form = useForm<FormValues>({
resolver: zodResolver(formSchema),
defaultValues: { username: "", email: "" },
})
async function onSubmit(data: FormValues) {
// Handle submission
}
return (
<Form {...form}>
<form onSubmit={form.handleSubmit(onSubmit)} className="space-y-6">
<FormField
control={form.control}
name="username"
render={({ field }) => (
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input placeholder="johndoe" {...field} />
</FormControl>
<FormMessage />
</FormItem>
)}
/>
<Button type="submit" disabled={form.formState.isSubmitting}>
{form.formState.isSubmitting ? "Submitting..." : "Submit"}
</Button>
</form>
</Form>
)
}
```
For complete form examples (Login, Registration, Profile, Contact, Multi-Step), see [references/form-examples.md](references/form-examples.md).
## Form Field Types
Each field type follows the same `FormField` → `render` pattern with different components:
| Field Type | Component | Install |
|-----------|-----------|---------|
| Text Input | `Input` | `npx shadcn@latest add input` |
| Select | `Select` + `SelectContent` + `SelectItem` | `npx shadcn@latest add select` |
| Checkbox | `Checkbox` | `npx shadcn@latest add checkbox` |
| Textarea | `Textarea` | `npx shadcn@latest add textarea` |
| Switch | `Switch` | `npx shadcn@latest add switch` |
| Date Picker | `Calendar` + `Popover` | `npx shadcn@latest add popover calendar` |
For field-specific implementation patterns, see [references/form-examples.md](references/form-examples.md).
## Zod Validation Patterns
Common schema patterns:
- **Registration** — Password confirmation with `.refine()`, regex rules
- **Contact** — Required strings with min lengths
- **Profile** — Optional fields with `.optional()`, arrays with `z.array()`
For 15+ validation patterns (string, number, date, array, object, async, transform, discriminated union, file, error customization), see [references/validation-patterns.md](references/validation-patterns.md).
## Form Patterns
| Pattern | Key Technique |
|---------|--------------|
| Async Validation | `mode: "onBlur"` + `form.setError()` after API check |
| Multi-Step | `form.trigger(fields)` to validate current step before advancing |
| Loading State | `useState(isLoading)` + disable submit button during request |
For implementations, see [references/form-examples.md](references/form-examples.md).
## Reference Files
| File | Contents | Read when... |
|------|----------|-------------|
| [references/form-examples.md](references/form-examples.md) | Login, Registration, Profile Settings, Contact with File Upload, Multi-Step Form, Date Picker, Loading State | Building a complete form or looking for field-type patterns |
| [references/validation-patterns.md](references/validation-patterns.md) | 15+ Zod patterns: string, number, date, array, object, async, transform, discriminated union, file, error customization | Writing complex validation schemas |
## Resources
- react-hook-form: https://react-hook-form.com
- Zod: https://zod.dev
- shadcn Form docs: https://ui.shadcn.com/docs/components/form
Related in Web Dev
generating-lwc-components
IncludedLightning Web Components with PICKLES methodology and 165-point scoring. Use this skill when the user creates or edits LWC components, builds wire service patterns, or writes Jest tests for LWC. TRIGGER when: user creates/edits LWC components, touches lwc/**/*.js, .html, .css, .js-meta.xml files, or asks about wire service, SLDS, or Jest LWC tests. DO NOT TRIGGER when: Apex classes (use generating-apex), Aura components, or Visualforce.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Set up queries with useQuery, mutations with useMutation, configure QueryClient caching strategies, implement optimistic updates, and handle infinite scroll with useInfiniteQuery. Use when: setting up data fetching in React projects, migrating from v4 to v5, or fixing object syntax required errors, query callbacks removed issues, cacheTime renamed to gcTime, isPending vs isLoading confusion, keepPreviousData removed problems.
document-processor-api
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
nutrient-document-processing
IncludedProcess documents with Nutrient DWS. Use when the user wants to generate PDFs from HTML or URLs, convert Office/images/PDFs, assemble or split packets, OCR scans, extract text/tables/key-value pairs, redact PII, watermark, sign, fill forms, optimize PDFs, or produce compliance outputs like PDF/A or PDF/UA. Triggers include convert to PDF, merge these PDFs, OCR this scan, extract tables, redact PII, sign this PDF, make this PDF/A, or linearize for web delivery.
tanstack-query
IncludedManage server state in React with TanStack Query v5. Covers useMutationState, simplified optimistic updates, throwOnError, network mode (offline/PWA), and infiniteQueryOptions. Use when setting up data fetching, fixing v4→v5 migration errors (object syntax, gcTime, isPending, keepPreviousData), or debugging SSR/hydration issues with streaming server components.
accelint-nextjs-best-practices
IncludedNext.js performance optimization and best practices. Use when writing Next.js code (App Router or Pages Router); implementing Server Components, Server Actions, or API routes; optimizing RSC serialization, data fetching, or server-side rendering; reviewing Next.js code for performance issues; fixing authentication in Server Actions; or implementing Suspense boundaries, parallel data fetching, or request deduplication.