Generate React forms with react-hook-form and Zod: shared schemas, labelled field components, accessible error messages, async validation, submission state and tests.
.devin/rules/windsurf-form-handling-patterns.md. Downloaded files need to be placed in that repository path.---
trigger: glob
globs: "**/*"
---
When generating form code in React:
## react-hook-form
- Use `useForm` with a schema resolver, `register` for native inputs and `Controller` for custom components. Set `defaultValues` for every field.
- Choose `mode` deliberately: `onSubmit` (default) or `onTouched`/`onBlur` for long forms; avoid validating on every keystroke unless needed.
## Zod schemas
- Define one schema per form and derive the TypeScript type from it; reuse the same schema on the server to validate the submission.
```ts
import { z } from 'zod';
export const SignupSchema = z
.object({
email: z.email('Enter a valid email'),
password: z.string().min(8, 'Use at least 8 characters'),
confirmPassword: z.string(),
})
.refine((v) => v.password === v.confirmPassword, {
path: ['confirmPassword'],
message: 'Passwords do not match',
});
export type SignupValues = z.infer<typeof SignupSchema>;
```
- Connect it with `zodResolver` from `@hookform/resolvers/zod`; check that the installed resolvers version supports your Zod major version.
## Field components and errors
- Every field has a `<label>` tied to the input, an error message element linked with `aria-describedby`, and `aria-invalid` when invalid.
```tsx
<label htmlFor="email">Email</label>
<input id="email" type="email" autoComplete="email" {...register('email')}
aria-invalid={errors.email ? 'true' : 'false'} aria-describedby="email-error" />
{errors.email && <p id="email-error" role="alert">{errors.email.message}</p>}
```
- Move focus to the first invalid field on submit (react-hook-form does this by default with `shouldFocusError`).
## Async validation
- Run server checks (for example "email already registered") on blur or submit with debouncing, and show server-side errors with `setError('email', { message })` after submission.
## Submission
- Use `handleSubmit(onValid)`, disable the submit button while `formState.isSubmitting`, show a success or error message, and handle network failures without clearing the user's input.
## Tests
- Test with React Testing Library and user-event: fill fields by label, submit, and assert validation messages, the submitted values and server error display.
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor rules for React with TypeScript: typed function components, React 19 ref-as-prop, hooks discipline, state ownership, error boundaries and measured performance work.
CLAUDE.md rules for React 19 with TypeScript covering typed components, derived state, compiler-aware memoization, useId-based accessibility and review checks.
Windsurf
frontend
Illustrative input and expected result. This example is a verification guide, not a recorded model run.
Content reviewed .
A Windsurf React app needs a signup form with password confirmation, a server check for duplicate emails and accessible error messages.
Generate SignupForm.tsx, its Zod schema and tests using the form handling rule (react-hook-form, Zod 4).
A shared SignupSchema with a refine on confirmPassword, a form using useForm + zodResolver with defaultValues, labelled inputs with aria-invalid/aria-describedby, setError for a duplicate-email server response, a submit button disabled while submitting, and tests that fill fields by label and assert messages.
Windsurf's rules documentation (docs.windsurf.com/windsurf/cascade/memories) now redirects to Devin documentation, which describes rules for the Devin Local agent in Devin Desktop. Current rules live in .devin/rules/*.md with a trigger setting, and the legacy Windsurf files (.windsurf/rules/*.md, .windsurf/global_rules.md and .windsurfrules) are still imported by default. AGENTS.md files are read as always-on rules.
.devin/rules/windsurf-form-handling-patterns.mdStarting point, not a tested configuration for your repository. Applied when files matching globs are involved (trigger: glob). Review the instructions, adapt the file paths and run your project's existing checks.
---
trigger: glob
globs: "**/*"
---
When generating form code in React:
## react-hook-form
- Use `useForm` with a schema resolver, `register` for native inputs and `Controller` for custom components. Set `defaultValues` for every field.
- Choose `mode` deliberately: `onSubmit` (default) or `onTouched`/`onBlur` for long forms; avoid validating on every keystroke unless needed.
## Zod schemas
- Define one schema per form and derive the TypeScript type from it; reuse the same schema on the server to validate the submission.
```ts
import { z } from 'zod';
export const SignupSchema = z
.object({
email: z.email('Enter a valid email'),
password: z.string().min(8, 'Use at least 8 characters'),
confirmPassword: z.string(),
})
.refine((v) => v.password === v.confirmPassword, {
path: ['confirmPassword'],
message: 'Passwords do not match',
});
export type SignupValues = z.infer<typeof SignupSchema>;
```
- Connect it with `zodResolver` from `@hookform/resolvers/zod`; check that the installed resolvers version supports your Zod major version.
## Field components and errors
- Every field has a `<label>` tied to the input, an error message element linked with `aria-describedby`, and `aria-invalid` when invalid.
```tsx
<label htmlFor="email">Email</label>
<input id="email" type="email" autoComplete="email" {...register('email')}
aria-invalid={errors.email ? 'true' : 'false'} aria-describedby="email-error" />
{errors.email && <p id="email-error" role="alert">{errors.email.message}</p>}
```
- Move focus to the first invalid field on submit (react-hook-form does this by default with `shouldFocusError`).
## Async validation
- Run server checks (for example "email already registered") on blur or submit with debouncing, and show server-side errors with `setError('email', { message })` after submission.
## Submission
- Use `handleSubmit(onValid)`, disable the submit button while `formState.isSubmitting`, show a success or error message, and handle network failures without clearing the user's input.
## Tests
- Test with React Testing Library and user-event: fill fields by label, submit, and assert validation messages, the submitted values and server error display.
Ask for a small change to one matching file. Check the tool's loaded context or instruction view, inspect the diff for the requested convention, and run the relevant build or test. A well-formatted file does not guarantee the model follows every instruction.
The docs recommend keeping rules short and specific. The current docs say AGENTS.md, AGENTS.local.md and AGENT.md are read as always-on rules, including files in subdirectories. Use .devin/rules/ when you need glob, model-decision or manual triggers.
Documentation checked 2026-11-05: Devin docs: Local customizations (redirect target of Windsurf memories docs)Windsurf docs: Cascade memories (redirects)
Cursor rules for Tailwind CSS v4: automatic class sorting, mobile-first breakpoints, CVA variants with merging, class-based dark mode via @custom-variant, and CSS-first @theme configuration.
Cursor rules for React with TypeScript: typed function components, React 19 ref-as-prop, hooks discipline, state ownership, error boundaries and measured performance work.
CLAUDE.md rules for React 19 with TypeScript covering typed components, derived state, compiler-aware memoization, useId-based accessibility and review checks.