$npx @dinachi/cli@latest add form
validationModeonSubmit
onBlur
onChange
passwordsign-up-form.tsx
import { useState } from "react"
import { Button } from "@/components/ui/button"
import { Field, FieldControl, FieldError, FieldLabel } from "@/components/ui/field"
import { Form } from "@/components/ui/form"
export function SignUpForm() {
const [done, setDone] = useState<string | null>(null)
return (
<Form
onFormSubmit={(values) => setDone(String(values.username))}
className="w-full max-w-sm space-y-4"
>
<Field
name="username"
validate={(value) => {
const s = String(value ?? "")
if (!s) return "Username is required"
return s.length < 3 ? "At least 3 characters" : null
}}
>
<FieldLabel>Username</FieldLabel>
<FieldControl placeholder="Choose a username" />
<FieldError />
</Field>
<Field
name="password"
validate={(value) => (String(value ?? "").length < 8 ? "At least 8 characters" : null)}
>
<FieldLabel>Password</FieldLabel>
<FieldControl type="password" placeholder="Enter password" />
<FieldError />
</Field>
<div className="flex items-center gap-3">
<Button type="submit">Sign Up</Button>
{done && <span className="text-sm text-muted-foreground">Welcome, {done}.</span>}
</div>
</Form>
)
}Usage
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
Return errors from onFormSubmit and show them per field.
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>
);
}Required fields checked on submit.
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>
);
}Several fields with submit and reset.
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.
<Form errors={{ email: "That address is taken" }} onFormSubmit={save}>
<Field name="email">
<FieldLabel>Email</FieldLabel>
<FieldControl type="email" />
<FieldError />
</Field>
</Form>API
onFormSubmit(values, details) => void
Receives values keyed by Field name. Prevents the native submit.errorsRecord<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.actionsRefRefObject<{ validate }>
validate() checks every field, validate(name) checks one.onSubmit(event) => void
The native handler. Runs only when every field is valid.aria-label
Names the form. The agent reports it as agent
stringin 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.