GooseUI

Components

Form

New

A form element with spacing, built with Field. Validation is native: required, minLength, pattern, type="email". The browser stops the submit, focuses the first bad field and Field shows the error. No react-hook-form, no state. Server component; handle the submit with action or with onSubmit from a client component.

Browser Support

Baseline Widely available
1 feature
Details
:user-valid and :user-invalid

Errors appear only after the user touched a field, not on page load. Without it the errors stay hidden; the submit is still blocked by the browser

Your browser:

Preview

3 to 16 characters: a-z, 0-9, underscore.

Installation

npx shadcn@latest add @gooseui/form
pnpm dlx shadcn@latest add @gooseui/form
yarn dlx shadcn@latest add @gooseui/form
bunx --bun shadcn@latest add @gooseui/form

Usage

"use client"

import { Button } from "@/components/ui/button"
import { Field, FieldError, FieldLabel } from "@/components/ui/field"
import { Form, FormActions } from "@/components/ui/form"
import { Input } from "@/components/ui/input"

export default function Example() {
  return (
    <Form
      onSubmit={(event) => {
        event.preventDefault()
        const data = new FormData(event.currentTarget)
        console.log(Object.fromEntries(data))
      }}
    >
      <Field>
        <FieldLabel htmlFor="email">Email</FieldLabel>
        <Input id="email" name="email" type="email" required />
        <FieldError>Enter a valid email</FieldError>
      </Field>
      <FormActions>
        <Button type="submit">Send</Button>
      </FormActions>
    </Form>
  )
}

Examples

Server action

Pass action; the form is a plain server component and works without JavaScript.

async function subscribe(formData: FormData) {
  "use server"
  await save(String(formData.get("email")))
}

<Form action={subscribe}>
  <Field>
    <FieldLabel htmlFor="email">Email</FieldLabel>
    <Input id="email" name="email" type="email" required />
    <FieldError>Enter a valid email</FieldError>
  </Field>
  <Button type="submit">Subscribe</Button>
</Form>

Constraint attributes

Rules live in HTML attributes of the control. The browser checks them before submit.

<Input required />                         // must not be empty
<Input type="email" />                     // email format
<Input type="url" />                       // URL format
<Input minLength={2} maxLength={40} />     // text length
<Input type="number" min={1} max={10} />   // number range
<Input pattern="[a-z0-9_]{3,16}" />        // regular expression

Props

PropTypeDefaultDescription
onSubmit(event: FormEvent) => void-Runs after native validation passed; call preventDefault to handle it in the browser
actionstring | ((formData: FormData) => void)-Server action or URL the form is sent to
noValidatebooleanfalseTurns the browser validation off (not recommended)
classNamestring-Additional CSS classes