# Form
A native form that validates Fields and shows server errors.
Source page: https://dinachi.dev/docs/components/form

## Installation
```bash
npx @dinachi/cli@latest add form
```
Adds to your project:
- `components/ui/form/form.tsx`
- `components/ui/form/index.ts`
Installs:
- `@base-ui/react` ^1.9.0

## Usage
```tsx
import { Button } from "@/components/ui/button"
import { Field, FieldControl, FieldError, FieldLabel } from "@/components/ui/field"
import { Form } from "@/components/ui/form"

export function SignIn() {
  return (
    <Form onFormSubmit={(values) => console.log(values.email)}>
      <Field name="email">
        <FieldLabel>Email</FieldLabel>
        <FieldControl type="email" required />
        <FieldError />
      </Field>
      <Button type="submit">Sign in</Button>
    </Form>
  )
}
```

## Examples

### Validation
Return errors from onFormSubmit and show them per field.
```tsx
import React from 'react';
import { Form } from '@/components/ui/form';
import { Field, FieldLabel, FieldControl, FieldError } from '@/components/ui/field';
import { Button } from '@/components/ui/button';

export function Example() {
  const [errors, setErrors] = React.useState({});

  const handleFormSubmit = (values) => {
    const newErrors = {};
    if (!values.username) newErrors.username = 'Username is required';
    if (!values.password) newErrors.password = 'Password is required';
    setErrors(newErrors);
  };

  return (
    <Form errors={errors} onFormSubmit={handleFormSubmit} className="w-full max-w-sm space-y-4">
      <Field name="username" invalid={!!errors.username}>
        <FieldLabel>Username</FieldLabel>
        <FieldControl placeholder="Choose a username" />
        {errors.username && <FieldError>{errors.username}</FieldError>}
      </Field>
      <Field name="password" invalid={!!errors.password}>
        <FieldLabel>Password</FieldLabel>
        <FieldControl type="password" placeholder="Enter password" />
        {errors.password && <FieldError>{errors.password}</FieldError>}
      </Field>
      <Button type="submit">Sign Up</Button>
    </Form>
  );
}
```

### Default
Required fields checked on submit.
```tsx
import { Form } from '@/components/ui/form';
import { Field, FieldLabel, FieldControl } from '@/components/ui/field';
import { Button } from '@/components/ui/button';

export function Example() {
  return (
    <Form className="w-full max-w-sm space-y-4">
      <Field name="name">
        <FieldLabel>Name</FieldLabel>
        <FieldControl placeholder="Enter your name" required />
      </Field>
      <Field name="email">
        <FieldLabel>Email</FieldLabel>
        <FieldControl type="email" placeholder="you@example.com" required />
      </Field>
      <Button type="submit">Submit</Button>
    </Form>
  );
}
```

### Contact form
Several fields with submit and reset.
```tsx
import { Form } from '@/components/ui/form';
import { Field, FieldLabel, FieldControl } from '@/components/ui/field';
import { Button } from '@/components/ui/button';

export function Example() {
  return (
    <Form className="w-full max-w-sm space-y-4">
      <Field name="firstName">
        <FieldLabel>First Name</FieldLabel>
        <FieldControl placeholder="John" />
      </Field>
      <Field name="lastName">
        <FieldLabel>Last Name</FieldLabel>
        <FieldControl placeholder="Doe" />
      </Field>
      <Field name="email">
        <FieldLabel>Email</FieldLabel>
        <FieldControl type="email" placeholder="john@example.com" />
      </Field>
      <div className="flex gap-2">
        <Button type="submit">Send</Button>
        <Button type="reset" variant="outline">Reset</Button>
      </div>
    </Form>
  );
}
```

## Notes

### Submit
Every Field validates. If any is invalid, submit stops and focus moves to the first invalid control. Otherwise `onSubmit` runs, then `onFormSubmit`. Browser validation bubbles are off (`noValidate`); Fields show the messages.

### Server errors
Set `errors` after a request fails, keyed by Field `name`. Each Field's `FieldError` shows its message, focus moves to the first errored field, and an error clears once its control changes.

```tsx
<Form errors={{ email: "That address is taken" }} onFormSubmit={save}>
  <Field name="email">
    <FieldLabel>Email</FieldLabel>
    <FieldControl type="email" />
    <FieldError />
  </Field>
</Form>
```

## API reference

### Form
A `<form>` that validates its Fields.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `onFormSubmit` | `(values, details) => void` | - | Receives values keyed by Field `name`. Prevents the native submit. |
| `errors` | `Record<string, string \| string[]>` | - | Errors from a server or form action, keyed by Field `name`. |
| `validationMode` | `"onSubmit" \| "onBlur" \| "onChange"` | `"onSubmit"` | When Fields validate. A Field's own setting wins. |
| `actionsRef` | `RefObject<{ validate }>` | - | `validate()` checks every field, `validate(name)` checks one. |
| `onSubmit` | `(event) => void` | - | The native handler. Runs only when every field is valid. |
| `aria-label` **agent** | `string` | - | Names the form. The agent reports it as `in` on each control inside. |

## Accessibility
- Renders a native `<form>`, so Enter in a text field submits it.
- An invalid submit focuses the first invalid control, announcing its error.
- With several forms on a page, give each an aria-label; agents report it as `in`.

## Source
`components/ui/form/form.tsx`
```tsx
"use client"

import * as React from "react"
import { Form as BaseForm } from "@base-ui/react/form"
import { cn } from "@/lib/utils"

export type FormProps = React.ComponentProps<typeof BaseForm>

const Form = React.forwardRef<
  HTMLFormElement,
  FormProps
>(({ className, ...props }, ref) => (
  <BaseForm
    ref={ref}
    data-slot="form"
    className={cn("space-y-4", className)}
    {...props}
  />
))

Form.displayName = "Form"

export { Form }
```
