Skip to content

Data Input

Form

Native-constraint form that focuses the first invalid field.

import { Form } from '@var-ui/react';

Form

Native-constraint form that focuses the first invalid field. Form is a React <form> that runs checkValidity() and accepts a server errors map keyed by field name. A truthy errors entry blocks submit; the app owns clearing it — Form does not reset errors when the control changes.

There is no @var-ui/astro Form. Native required works in Astro without Form; focus-first-invalid is React-only.

Examples

Default

Submit with empty required fields to see native messages and focus on the first invalid control.

Demo.tsxTSX
import { Button, Field, Form } from '@var-ui/react';

<Form>
  <Field.Root name="email">
    <Field.Label>Email</Field.Label>
    <Field.Control>
      <input name="email" type="email" required />
    </Field.Control>
    <Field.Error />
  </Field.Root>
  <Field.Root name="name">
    <Field.Label>Name</Field.Label>
    <Field.Control>
      <input name="name" required />
    </Field.Control>
    <Field.Error />
  </Field.Root>
  <Button type="submit">Save</Button>
</Form>

Server errors and pending

An async onSubmit with a server errors map and a pending button. Enter taken@example.com to see the server error.

Try taken@example.com to see a server error.

Demo.tsxTSX
import { useState } from 'react';
import { Button, Form, TextField } from '@var-ui/react';

const TAKEN = 'taken@example.com';

// Stands in for your API: resolves with field errors keyed by input name.
async function createAccount(data: FormData): Promise<Record<string, string>> {
  await new Promise((resolve) => setTimeout(resolve, 800));
  return data.get('email') === TAKEN ? { email: 'That email is already registered' } : {};
}

export default function Preview() {
  const [errors, setErrors] = useState<Record<string, string>>({});
  const [pending, setPending] = useState(false);
  const [created, setCreated] = useState(false);

  async function onSubmit(_event: unknown, data: FormData) {
    setPending(true);
    setCreated(false);
    try {
      const result = await createAccount(data);
      setErrors(result);
      setCreated(Object.keys(result).length === 0);
    } finally {
      setPending(false);
    }
  }

  return (
    <Form errors={errors} onSubmit={onSubmit} onError={() => setErrors({ email: 'Network error' })}>
      <TextField
        name="email"
        type="email"
        label="Email"
        description={`Try ${TAKEN} to see a server error.`}
        required
        errorMessage={errors.email}
        onChange={() => setErrors({})}
      />
      <Button type="submit" loading={pending}>
        Create account
      </Button>
      {created ? <p role="status">Account created.</p> : null}
    </Form>
  );
}

Server errors

Pass an errors map keyed by field name. A truthy entry blocks submit until the app clears it.

TSX
<Form errors={{ email: 'Enter a valid email' }}>
  <TextField name="email" label="Email" />
</Form>

Using with react-hook-form

Form also wraps a react-hook-form handleSubmit(fn) and its setError for server errors; register() works on the text fields, Checkbox and Switch, and Controller covers Select, the date fields and the other composite controls. See the Forms guide for the table, Zod with zodResolver, and verified examples.

Props

Accessibility

Form is React-only; native required works in Astro without Form. On invalid submit, focus moves to the first [aria-invalid="true"] or :invalid control.