---
description: Create a type-safe form with TanStack Form and Zod validation
---
# Create Form
Generate a type-safe form using TanStack Form (@tanstack/react-form) with Zod validation and shadcn/ui components.
## Instructions
1. Install TanStack Form if not already installed:
```bash
bun add @tanstack/react-form zod
```
2. Optionally, install shadcn/ui components for UI:
```bash
bunx shadcn@latest add input button label
```
3. Ask for form fields and validation rules
4. Create Zod schema for form validation
5. Generate form component with:
- TanStack Form `useForm` hook
- Zod validators (`onChange`, `onSubmit`)
- `form.Field` components for each field
- Proper TypeScript types
- Error handling and display
- Submit handler with loading states
6. Add accessibility attributes (labels, ARIA)
7. Include validation feedback (errors, loading states)
## Form Example
### Basic TanStack Form with Zod Validation
```typescript
"use client";
import { useForm } from "@tanstack/react-form";
import { z } from "zod";
import { Input } from "@/shared/components/ui/input";
import { Button } from "@/shared/components/ui/button";
import { Label } from "@/shared/components/ui/label";
const loginSchema = z.object({
email: z.string().email("Invalid email address"),
password: z.string().min(8, "Password must be at least 8 characters"),
});
export function LoginForm() {
const form = useForm({
defaultValues: {
email: "",
password: "",
},
validators: {
onChange: loginSchema,
},
onSubmit: async ({ value }) => {
// Handle form submission
console.log("Form submitted:", value);
},
});
return (
)}
/>
[state.canSubmit, state.isSubmitting]}
children={([canSubmit, isSubmitting]) => (
)}
/>
);
}
```
## Key Features
- **Type-Safe**: Full TypeScript support with inferred types from Zod schemas
- **Validation**: Synchronous and asynchronous validation with debounce
- **Field-Level Validation**: `onChange`, `onBlur`, `onMount` validators per field
- **Form-Level Validation**: Custom cross-field validation on submit
- **State Management**: Built-in form state (canSubmit, isSubmitting, errors)
- **Performance**: Efficient re-renders with granular subscriptions
- **Framework Agnostic**: Works with React, Solid, Vue, and more
Ensure complete type safety and proper validation with TanStack Form.