---
title: Form for Astro
description: "Compose Astro forms with labels, inputs, validation messages, and buttons. See examples and install the source with the shadcn CLI."
seo:
    title: Form for Astro | Shadcn & Tailwind CSS
related:
    - /components/field
    - /components/button
    - /components/alert
sidebar:
    label: "Form"
---

`Form` is intentionally small. It is just a styled `<form>` element with vertical
spacing so you can compose regular Astro forms with `Field`, `Input`,
`Textarea`, `Checkbox`, `NativeSelect`, and `Button`.

## Usage

```ts
import { Form } from "@/components/ui/form"
```

```astro
---
import { Button } from "@/components/ui/button"
import {
  Field,
  FieldDescription,
  FieldGroup,
  FieldLabel,
} from "@/components/ui/field"
import { Form } from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
---

<Form method="post">
  <FieldGroup>
    <Field>
      <FieldLabel for="name">Name</FieldLabel>
      <Input id="name" name="name" />
    </Field>
    <Field>
      <FieldLabel for="message">Message</FieldLabel>
      <Textarea id="message" name="message" />
      <FieldDescription>Tell us what you are building.</FieldDescription>
    </Field>
  </FieldGroup>

  <Button type="submit">Send</Button>
</Form>
```

## Example

```astro
---
import { Button } from "@/components/ui/button"
import {
  Field,
  FieldDescription,
  FieldGroup,
  FieldLabel,
} from "@/components/ui/field"
import { Form } from "@/components/ui/form"
import { Input } from "@/components/ui/input"
import { Textarea } from "@/components/ui/textarea"
---

<Form class="w-full max-w-xl">
  <FieldGroup>
    <Field>
      <FieldLabel for="contact-name">Name</FieldLabel>
      <Input id="contact-name" name="name" placeholder="Ada Lovelace" />
    </Field>
    <Field>
      <FieldLabel for="contact-email">Email</FieldLabel>
      <Input
        id="contact-email"
        type="email"
        name="email"
        placeholder="ada@example.com"
      />
    </Field>
    <Field>
      <FieldLabel for="contact-message">Message</FieldLabel>
      <Textarea
        id="contact-message"
        name="message"
        placeholder="Tell us about your project"
      />
      <FieldDescription>
        These are regular Astro form controls.
      </FieldDescription>
    </Field>
  </FieldGroup>

  <Button type="submit" class="w-fit">
    Send message
  </Button>
</Form>
```

## Installation

### Command

```sh
bunx --bun shadcn@latest add @gnostic/form
```

### Manual

Complete the [Installation guide](/docs/installation) first. Then copy the component files below.

No extra packages are required.

#### src/components/ui/form/form.astro

```astro
---
import type { HTMLAttributes } from "astro/types"

import { cn } from "cn"

type Props = HTMLAttributes<"form">

const { class: className, ...props } = Astro.props
---

<form data-slot="form" class={cn("flex flex-col gap-6", className)} {...props}>
  <slot />
</form>
```

#### src/components/ui/form/index.ts

```ts
export { default as Form } from "./form.astro"
```

Component classes use the preset configured during project setup. Select the same preset in the docs before copying Manual source. To change installed component classes, edit or reinstall their source. See [Theming](/docs/theming) for details.


## Notes

- Use plain Astro form attributes like `method`, `action`, and `netlify` directly on `Form`.
- Use `Field` and `FieldGroup` for structure instead of generated form schemas.
- If you do not need the default spacing, use a plain `<form>` element instead.

## API Reference

Read the [installed source](/r/form.json) for the complete prop types.

## When to use Form

Form renders a real form. Set its method and action for your server endpoint or Astro action, then compose Field and native controls inside it. Give submitted controls names and choose an explicit submit button.

## Keyboard and behavior

The wrapper does not validate business rules, send requests, or manage pending state. Native browser constraints still apply. Return field errors from the server and retain entered values when a submission fails.

## Theme and layout

Spacing comes from the wrapper; controls use their own shared tokens. Avoid nested forms. A form can contain several fieldsets without changing how values are submitted.
