# Add Form Validation to a React Router App

Yes. An agent adds a contact route with a server action, a shared zod schema, field errors and a typed Form to a React Router 7 app, tests it and opens a PR.

[Run this on Rush](https://cloud.getrush.ai/run/add-form-validation/react-router/). Opens the console with this prompt ready to send, in a Project that has your repository.

## What the agent does

The agent reads what the app renders and how it is checked before it adds a form. In an app shaped like the `default` template of remix-run/react-router-templates, `package.json` runs `react-router build`, `react-router dev`, `react-router-serve ./build/server/index.js` and `react-router typegen && tsc` for `typecheck`, on `react` ^19.2.8, `react-router`, `vite` ^8.0.3, `tailwindcss` ^4.2.2 and `typescript` ^5.9.3. `react-router.config.ts` keeps `ssr: true`. `app/routes.ts` has one entry, the index route for `routes/home.tsx`, and `app/root.tsx` renders the `Layout` and an `ErrorBoundary`. There is no form, no `action` and no validation library. The repository's own check lives in `.tests/test.default.ts`: a Playwright run that copies the template into a temporary directory, runs `pnpm typecheck`, starts `pnpm dev` and then `pnpm build` plus `pnpm start`, loads the page, expects the title `New React Router App`, waits for the `React Router Docs` and `Join Discord` links, and fails on any page error. `.github/workflows/ci.yml` runs it on Node 22 with pnpm 10 and Chromium.

It adds `zod` as the one new dependency and puts the rules in one place, `app/lib/contact.ts`: a schema with `name` of 2 to 80 characters, `email` as a valid address and `message` of 20 to 2000 characters, plus a `parseContact(formData)` helper that returns either the typed values or a map of field errors built from the parse result. `app/routes/contact.tsx` exports an `action` that reads `request.formData()`, calls the helper and returns `data({ errors, values }, { status: 400 })` on failure or `redirect('/contact?sent=1')` on success, and a `clientAction` that runs the same schema in the browser and only calls `serverAction()` when the values pass, so a user sees errors without a round trip and the server still enforces every rule. The component renders `<Form method='post' noValidate>`, reads `actionData` from `Route.ComponentProps`, repopulates each `defaultValue` from the returned values, marks a failing input with `aria-invalid` and `aria-describedby` pointing at its error text, and disables the button while `useNavigation().state` is `submitting`. `app/routes.ts` gains a `route('contact', 'routes/contact.tsx')` entry, and `welcome.tsx` gets a `Contact` item in its list after the two links the test waits for.

`pnpm typecheck` regenerates the `+types/contact` file the component imports before `tsc` runs, so the action's return type and the component's props are checked together. The agent adds `.tests/test.contact.ts` with the template's `testTemplate('default')` fixture: it submits the empty form and expects three error messages and a 400 response, fills the fields and expects the sent state, and asserts `page.errors` is empty. It runs `pnpm typecheck`, `pnpm build` and, in `.tests`, `pnpm playwright install --with-deps chromium` and `pnpm test --project=chromium`, which runs the existing `test.default.ts` too. It also posts an invalid body with `curl` to the started server and pastes the 400 JSON in the pull request.

## Start it with one API call

`POST https://api.phoenix.so/o/{org}/sessions`

```json
{
  "agent": "claude",
  "input": "Add a contact form with validation to this React Router 7 app and open a pull request. Read package.json, react-router.config.ts, app/routes.ts, app/root.tsx, app/routes/home.tsx, app/welcome/welcome.tsx, tsconfig.json, .tests/test.default.ts, .tests/utils.ts and .github/workflows/ci.yml first. Add zod only. Put the schema and a parseContact(formData) helper in app/lib/contact.ts with name, email and message rules. Add app/routes/contact.tsx with an action that returns data({ errors, values }, { status: 400 }) on failure and redirects to /contact?sent=1 on success, a clientAction that runs the same schema and calls serverAction() only when valid, and a component using Form with noValidate, actionData from Route.ComponentProps, defaultValue from the returned values, aria-invalid and aria-describedby on failing fields and a disabled button while submitting. Register the route in app/routes.ts and add a Contact link to welcome.tsx after the existing two. Add .tests/test.contact.ts using testTemplate covering the empty submit, the valid submit and no page errors. Run pnpm typecheck, pnpm build and the Playwright suite in .tests with --project=chromium, post an invalid body with curl, and paste the outputs in the pull request. Do not change root.tsx, the Dockerfile or the Tailwind setup.",
  "resources": [
    {
      "url": "https://github.com/your-org/your-app"
    }
  ]
}
```

1. **Get an API key.** In the console, open Settings, then API keys, and create a key for your org. Send it in the Authorization header as a Bearer token.
2. **Send the request.** POST the body in request.json to https://api.phoenix.so/o/{org}/sessions, with your repository as the resources URL. The run starts in its own cloud sandbox.
3. **Read what came back.** Follow the run on its live stream or wait for the run.completed webhook, then read the result and its trajectory.

## What comes back

What comes back is a pull request with one new dependency, `app/lib/contact.ts`, `app/routes/contact.tsx` and `.tests/test.contact.ts`, plus edits to `app/routes.ts` and `app/welcome/welcome.tsx`. The description shows the 400 response from `curl`, the Playwright summary with both test files, and the `typecheck` and `build` output.

On a repository with the `CI` workflow, the pull request runs the Playwright suite in `.tests` on Chromium, which now includes the contact test beside the template's own, so a schema change that broke the page would fail there.

A reviewer checks that the action and the `clientAction` share one schema rather than two copies, that failure answers 400 and success redirects instead of re-rendering the POST, that the error ids are unique per field, and that the values echoed into `defaultValue` are the user's own and nothing else. Then they wire the success path to wherever messages should go.

## Run it on every new form in the app

Once the pattern is in, every later form is one more run: `POST /o/{org}/sessions` with a prompt naming the fields and their rules and the repository in `resources`. The agent adds the schema beside `contact.ts`, a route with the same `action` and `clientAction` shape and a test in `.tests`, and opens a pull request.

Give the API key a `webhook_url` and Rush POSTs `run.completed` when each turn ends, with the `session_url` to read the pull request from.

## Why run it on Rush?

Form validation lives in two places at once, the browser and the server, and the usual bug is that they disagree. In a Rush sandbox the agent starts the built app, submits the form through a real browser with Playwright and posts a raw body with `curl`, and keeps both in the run's record, so the pull request shows the 400 and the field errors from the server and from the browser rather than describing them.

The change uses React Router's own `action`, `clientAction`, `Form` and generated types, with one small dependency for the rules, so there is no form library to learn. The same request works on any React Router 7 app in framework mode.

## Frequently asked questions

### Why zod instead of HTML validation attributes alone?

Attributes such as `required` and `type='email'` only run in the browser and can be removed with the developer tools. The schema runs in the `action` on every submission and in the `clientAction` for fast feedback, so the same rules hold in both places. The form sets `noValidate` so the server's messages are the ones a user sees.

### Does the clientAction mean the server can be skipped?

No. The `clientAction` calls `serverAction()` whenever the values pass, and the `action` parses the body again. A request that bypasses the browser, such as the `curl` the agent sends, is answered with the same 400 and the same errors.

### Does the existing Playwright test still pass?

Yes. `.tests/test.default.ts` loads the index page, checks its title and the two links, and fails on page errors. The contact route is a new page, the welcome list keeps its first two links, and the new test file runs beside the old one.

### Can it use react-hook-form or Conform instead?

It can if the prompt asks. The default is the framework's `Form` and actions with one schema, because they need no client-side state library and keep the submission working before JavaScript loads.

## More React Router jobs

- [Set Up GitHub Actions CI for a React Router App](https://getrush.ai/run/add-github-actions-ci/react-router/)
- [Add Graceful Shutdown to a React Router App](https://getrush.ai/run/add-graceful-shutdown/react-router/)
- [Add a Health Check Route to a React Router App with a Custom Express Server](https://getrush.ai/run/add-health-checks/react-router/)

## The same job on other stacks

- [Add Form Validation to a SolidJS App](https://getrush.ai/run/add-form-validation/solidjs/)
- [Add Form Validation to a Vue App](https://getrush.ai/run/add-form-validation/vue/)
- [Add Form Validation to an Angular App](https://getrush.ai/run/add-form-validation/angular/)

## Keep reading

- [How a Rush run works on your repository, from request to pull request](https://getrush.ai/cloud/)
- [Compare cloud agents, agent sandboxes and AI code review](https://getrush.ai/compare/)
- [Rush pricing: Free, Pro and Max](https://getrush.ai/pricing/)
