# Form

A form that validates itself: fields report to it, it focuses the first that fails and hands you the values. No form library.

Docs: https://ballpoint.st9wd.com/docs/form

```bash
npx shadcn@latest add @ballpoint/form
```

```tsx
import { Form, FormDescription, FormField, FormLabel, FormMessage } from "@/components/ui/form"
import { Input } from "@/components/ui/input"

<Form onFormSubmit={(values) => save(values)}>
  <FormField name="email">
    <FormLabel>Email</FormLabel>
    <Input type="email" required />
    <FormMessage match="valueMissing">We need somewhere to write to.</FormMessage>
  </FormField>
</Form>
```

Props:

- `Form onFormSubmit`: `(values, details) => void`. Called with every field's value, keyed by name, once they all validate.
- `Form errors`: `Record<string, string | string[]>`. Errors from outside (a server), keyed by the field's name.
- `Form validationMode`: `"onSubmit" | "onBlur" | "onChange"` (default `"onSubmit"`). When fields are first checked.
- `FormField name / validate`: `string / (value) => string | null`. The field's key in the values, and a check of your own.
- `FormMessage match`: `string | boolean`. Show only for one kind of failure, such as "valueMissing" or "typeMismatch".

Every other prop goes to Base UI's Form: https://base-ui.com/react/components/form

## Example

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Form, FormDescription, FormField, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";

export default function FormDemo() {
  const [sent, setSent] = useState<Record<string, unknown> | null>(null);
  return (
    <div className="flex w-full max-w-sm flex-col gap-5">
      <Form onFormSubmit={(values) => setSent(values)}>
        <FormField name="username">
          <FormLabel>Username</FormLabel>
          <Input required minLength={3} placeholder="ballpoint" seed="form-username" />
          <FormDescription>This is how you appear on the page.</FormDescription>
          <FormMessage match="valueMissing">Choose a username.</FormMessage>
          <FormMessage match="tooShort">At least three letters, please.</FormMessage>
        </FormField>
        <FormField name="email">
          <FormLabel>Email</FormLabel>
          <Input type="email" required placeholder="you@example.com" seed="form-email" />
          <FormMessage match="valueMissing">We need somewhere to write to.</FormMessage>
          <FormMessage match="typeMismatch">That email is missing its domain.</FormMessage>
        </FormField>
        <Button type="submit" className="self-start" seed="form-submit">
          Sign up
        </Button>
      </Form>
      {sent && (
        <pre className="text-sm text-ink-2" aria-live="polite">
          {JSON.stringify(sent, null, 2)}
        </pre>
      )}
    </div>
  );
}
```

## Errors from the server

Pass errors keyed by field name; they clear when the field is edited.

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Form, FormField, FormLabel, FormMessage } from "@/components/ui/form";
import { Input } from "@/components/ui/input";

export default function FormServerErrors() {
  const [errors, setErrors] = useState<Record<string, string>>({});
  return (
    <Form
      className="max-w-sm"
      errors={errors}
      onFormSubmit={(values) => {
        // Pretend the server already has this name. The error goes when the field is edited.
        setErrors(values.username === "ada" ? { username: "Someone already writes under that name." } : {});
      }}
    >
      <FormField name="username">
        <FormLabel>Username</FormLabel>
        <Input defaultValue="ada" seed="fse-username" />
        <FormMessage />
      </FormField>
      <Button type="submit" className="self-start" seed="fse-submit">
        Claim it
      </Button>
    </Form>
  );
}
```
