搜索

PythonReactSEOPDF
GitHub Skill

create-form

Set up hybrid client/server validated forms with Zod and React Hook Form. Use when creating forms with validation.

tech-with-seth☆ 1 仓库 Stars0 仓库 Forks

安装与使用

请根据来源仓库和你的 AI 工具说明安装此技能。先阅读技能文件和依赖要求,再添加到工作流。

查看来源 ↗使用指南

技能说明

Create Form

When to Use

  • Creating any form with validation
  • User asks to "add a form" or "create a form"

Critical Rule

// CORRECT - Use <form> with manual fetcher.submit()
<form onSubmit={handleSubmit(onSubmit)}>

// WRONG - Causes submission conflicts
<fetcher.Form onSubmit={handleSubmit(onSubmit)}>

Core Pattern

  1. Same Zod schema on client and server
  2. Client: Instant feedback with React Hook Form
  3. Server: Security with validateFormData()
  4. Auto error sync: Server errors populate form fields

Quick Start

1. Schema (app/lib/validations.ts)
import { z } from 'zod';

export const contactFormSchema = z.object({
    name: z.string().min(1, 'Name is required'),
    email: z.string().email('Invalid email'),
});

export type ContactFormData = z.infer<typeof contactFormSchema>;
2. Server Action
import { validateFormData } from '~/lib/form-validation.server';
import { zodResolver } from '@hookform/resolvers/zod';

export async function action({ request }: Route.ActionArgs) {
    const formData = await request.formData();
    const { data, errors } = await validateFormData<ContactFormData>(
        formData,
        zodResolver(contactFormSchema)
    );

    if (errors) return data({ errors }, { status: 400 });

    await processData(data!);
    return redirect('/success');
}
3. Client Form
import { useFetcher } from 'react-router';
import { useValidatedForm } from '~/lib/form-hooks';

export default function ContactPage() {
    const fetcher = useFetcher();
    const { register, handleSubmit, formState: { errors } } = useValidatedForm({
        resolver: zodResolver(contactFormSchema),
        errors: fetcher.data?.errors,
    });

    const onSubmit = (data: ContactFormData) => {
        const formData = new FormData();
        formData.append('name', data.name);
        fetcher.submit(formData, { method: 'POST' });
    };

    return (
        <form onSubmit={handleSubmit(onSubmit)}>
            <TextInput {...register('name')} error={errors.name?.message} />
            <Button type="submit">Submit</Button>
        </form>
    );
}

Checklist

  1. Define Zod schema in app/lib/validations.ts
  2. Add server action with validateFormData()
  3. Use useValidatedForm hook in component
  4. Use <form> (not <fetcher.Form>) with handleSubmit

Templates

Full Reference

See .github/instructions/form-validation.instructions.md for:

  • Field-level vs form-level errors
  • Conditional validation
  • File uploads
  • Complex schema patterns

使用示例

查看来源仓库中的使用示例与配置说明。

查看来源 ↗

来源历史

收录时间
2026-01-20
索引更新时间
2026-01-26
Branch
main

收录与同步日期属于本站记录。源码修改历史请查看来源仓库。

GitHub ↗

chat Comments (0)

chat_bubble_outline

No comments yet. Be the first to share your thoughts!