Form Creator
Instructions
1. Client-Side Forms
- Use
react-hook-formfor state management. - Use
zodfor schema validation. - Integrate using
@hookform/resolvers/zod. - Use the
Formcomponents from@/components/ui/form. - Define schemas using
z.object({...}).
2. API Validation
- MANDATORY: Use
zodschemas to validate incoming request bodies in API routes. - Share schemas between client and server if possible (e.g., in a separate file or exported from the component file if simple).
Example
Client
const form = useForm<z.infer<typeof schema>>({
resolver: zodResolver(schema),
defaultValues: { ... },
});
<Form {...form}>
<form
{/* ... fields ... */}
</form>
</Form>
Server (API Route)
import { z } from "zod";
const schema = z.object({
name: z.string().min(2),
});
export async function POST(req: Request) {
const body = await req.json();
const result = schema.safeParse(body);
if (!result.success) {
return NextResponse.json({ error: result.error }, { status: 400 });
}
// ... process valid data ...
}
Converted and distributed by TomeVault — claim your Tome and manage your conversions.