# Forms

Label fields, show validation errors, and save a form.

See [Build a form people can complete](/articles/accessible-forms) for labels, error messages, and keyboard use.

## Choose how to validate

For a small form, use [Form](/components/form) with Base UI validation. [Field](/components/field) connects a control to its label, help text, and error. Field.Control renders Ghost Input.

Add TanStack Form when you need typed values, coordinated validation, or subscriptions to form state.

## Connect TanStack Form

Install [Ghost UI and its component styles](/docs/installation#package), then add TanStack Form.

```bash
npm install @tanstack/react-form@1.33.5
```

Render ProjectForm with a save function that resolves after saving and rejects on failure. Submit with a blank name to see the field error. Then enter a name and save.

```tsx
import { useState } from "react";
import { useForm } from "@tanstack/react-form";
import { Form } from "@collectivecreation/ghost-ui/components/form";
import { Field } from "@collectivecreation/ghost-ui/components/field";
import { Button } from "@collectivecreation/ghost-ui/components/button";

export function ProjectForm({ save }: {
  save: (value: { title: string }) => Promise<void>;
}) {
  const [result, setResult] = useState("");
  const form = useForm({
    defaultValues: { title: "" },
    onSubmit: async ({ value }) => {
      setResult("");
      try { await save(value); setResult("Saved"); }
      catch { setResult("Could not save. Your input is still here."); }
    },
  });
  return <Form noValidate onSubmitCapture={event => {
    event.preventDefault();
    event.stopPropagation();
    if (!form.state.isSubmitting) void form.handleSubmit();
  }}>
    <form.Field name="title" validators={{
      onChange: ({ value }) => value.trim() ? undefined : "Enter a project name",
    }}>
      {field => {
        const errors = field.state.meta.errors;
        const invalid = errors.length > 0;
        return <Field.Root name={field.name} invalid={invalid}>
          <Field.Label>Project name</Field.Label>
          <Field.Control aria-required value={field.state.value} onBlur={field.handleBlur}
            onValueChange={value => { setResult(""); field.handleChange(value); }} />
          {invalid && <Field.Error match>{errors.join(". ")}</Field.Error>}
        </Field.Root>;
      }}
    </form.Field>
    <form.Subscribe selector={state => state.isSubmitting}>
      {busy => <Button type="submit" disabled={busy}>{busy ? "Saving…" : "Save"}</Button>}
    </form.Subscribe>
    <p role="status">{result}</p>
  </Form>;
}

```

TanStack Form runs validation and submission. The example uses noValidate and onSubmitCapture so native and Base UI validation stay out of that submission flow. Pass the TanStack error to Field.Error match, and set invalid from the same state so the control and error stay linked.

## Validate and save

Return a message from the validator when the input is invalid, or undefined when it passes. Validate again on the server. For schema or async validation, see [TanStack validation](https://tanstack.com/form/latest/docs/framework/react/guides/validation). Convert error objects to readable messages before rendering them.

Keep the entered values if a save fails. Disable repeat submissions while a save is pending. If the server returns a field error, show it beside the field and decide when to clear or revalidate it.

## Compose and extend

Use form.Subscribe to update the submit button as saving starts or ends. Connect custom controls through their own value and blur callbacks. Keep field components stable so typing preserves focus.

Use [Fieldset](/components/fieldset) to name a related group. Labels, layout and styling remain on the component pages. For exact hooks, types, field paths, arrays and reset behavior, use [TanStack Form](https://tanstack.com/form/latest/docs/framework/react/quick-start); for Ghost props, use [Form API](/components/form#api) and [Field API](/components/field#api).

## Troubleshooting

- Submit does nothing. Check that the capture handler calls handleSubmit and that only one validator controls submission.

- Typing leaves the value unchanged. Pass the current value and its change callback.

- An error stays hidden. Read invalid and Field.Error match from the same state.

- A different record shows old values. Reset the form explicitly when switching records and protect unsaved input from late fetches.
