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 available1 featureDetails
Feature
Baseline
Your browser
: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
Installation
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 expressionProps
| Prop | Type | Default | Description |
|---|---|---|---|
| onSubmit | (event: FormEvent) => void | - | Runs after native validation passed; call preventDefault to handle it in the browser |
| action | string | ((formData: FormData) => void) | - | Server action or URL the form is sent to |
| noValidate | boolean | false | Turns the browser validation off (not recommended) |
| className | string | - | Additional CSS classes |