Form

A native form that validates Fields and shows server errors.

Source
$npx @dinachi/cli@latest add form
validationMode
password

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>
  );
}

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
agent
string
Names the form. The agent reports it as in 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.