Conform

Progressive enhancement first form validation library for Remix and React R...

README

CONFORM


A progressive enhancement first form validation library for Remix and React Router

Highlights


- Focused on progressive enhancement by default
- Simplifed integration through event delegation
- Server first validation with Zod / Yup schema support
- Field name inference with type checking
- Focus management
- Accessibility support
- About 5kb compressed

Quick Start


Here is an example built with Remix:

  1. ```tsx
  2. import { useForm } from '@conform-to/react';
  3. import { parse } from '@conform-to/zod';
  4. import { Form } from '@remix-run/react';
  5. import { json } from '@remix-run/node';
  6. import { z } from 'zod';
  7. import { authenticate } from '~/auth';

  8. const schema = z.object({
  9.   email: z.string().min(1, 'Email is required').email('Email is invalid'),
  10.   password: z.string().min(1, 'Password is required'),
  11. });

  12. export async function action({ request }: ActionArgs) {
  13.   const formData = await request.formData();
  14.   const submission = parse(formData, { schema });

  15.   if (!submission.value || submission.intent !== 'submit') {
  16.     return json(submission, { status: 400 });
  17.   }

  18.   return await authenticate(submission.value);
  19. }

  20. export default function LoginForm() {
  21.   const lastSubmission = useActionData<typeof action>();
  22.   const [form, { email, password }] = useForm({
  23.     lastSubmission,
  24.     onValidate({ formData }) {
  25.       return parse(formData, { schema });
  26.     },
  27.   });

  28.   return (
  29.     <Form method="post" {...form.props}>
  30.       <div>
  31.         <label>Email</label>
  32.         <input type="email" name={email.name} />
  33.         <div>{email.error}</div>
  34.       </div>
  35.       <div>
  36.         <label>Password</label>
  37.         <input type="password" name={password.name} />
  38.         <div>{password.error}</div>
  39.       </div>
  40.       <button>Login</button>
  41.     </Form>
  42.   );
  43. }
  44. ```