Star on GitHub

Forms and validation

The form components work with the libraries you already use. In React, pair them with React Hook Form. In Vue, with VeeValidate. Zod holds the rules in both. Every example on this page runs as a test against the published packages.

How it fits#

  • Fields with a native input inside (Input, TextArea, Checkbox) take a ref and the usual input attributes, so React Hook Form's register() works on them as they are.
  • Custom widgets (DropdownField, DatePicker, OtpInput, Switch) are bound through Controller in React and v-model in Vue.
  • Fields show their own error. Pass the error state and the message, and the component marks the input invalid and links the message for screen readers. You don't wire any ARIA yourself.
Terminal
npm install react-hook-form @hookform/resolvers zod

A complete form#

A sign-up form with an email, a role picked from a dropdown and a terms checkbox. Submitting it empty shows all three errors. Filling it in submits the values.

// SignUpForm.tsx
import { Controller, useForm } from "react-hook-form";
import { zodResolver } from "@hookform/resolvers/zod";
import { z } from "zod";
import { CheckboxWithLabel, DropdownField, FlatButton, Input, SelectOption } from "@orvain/react";
const schema = z.object({
email: z.email("Enter a valid email address"),
role: z.string().min(1, "Pick a role"),
terms: z.boolean().refine((v) => v, "Accept the terms to continue"),
});
type Values = z.infer<typeof schema>;
export function SignUpForm({ onSubmit }: { onSubmit: (values: Values) => void }) {
const {
register,
control,
handleSubmit,
formState: { errors, isSubmitting },
} = useForm<Values>({
resolver: zodResolver(schema),
defaultValues: { email: "", role: "", terms: false },
});
return (
<form onSubmit={handleSubmit(onSubmit)} noValidate>
{/* a native input underneath: register() works as it is */}
<Input
label="Email address"
type="email"
required
{...register("email")}
state={errors.email ? "error" : "default"}
errorText={errors.email?.message}
/>
{/* a custom widget: bind it through Controller */}
<Controller
name="role"
control={control}
render={({ field, fieldState }) => (
<DropdownField
label="Role"
placeholder="Choose a role"
required
name={field.name}
value={field.value}
onValueChange={field.onChange}
onBlur={field.onBlur}
ref={field.ref}
state={fieldState.error ? "error" : "default"}
errorText={fieldState.error?.message}
>
<SelectOption value="design" label="Designer" />
<SelectOption value="eng" label="Engineer" />
</DropdownField>
)}
/>
<CheckboxWithLabel label="I accept the terms" {...register("terms")} errorText={errors.terms?.message} />
<FlatButton type="submit" label="Create account" disabled={isSubmitting} />
</form>
);
}
Mark a field required to drop the “(Optional)” note after its label. Keep noValidate on the form, so your schema decides what is valid, not the browser.

The test that proves it is in the testing guide.

Showing errors#

Pass the field's state and its message. The component sets aria-invalid on the input, points aria-describedby at the message, and announces the message when it appears. Most fields use state and errorText; the few that differ are in the table below.

<Input
label="Email address"
{...register("email")}
state={errors.email ? "error" : "default"}
errorText={errors.email?.message}
/>

Input also takes state="success" with successText, and helperText for a hint shown while there is no error.

What each field binds to#

ComponentReact Hook FormVueError props
Input
register()
v-model
state, errorText
TextArea (variant v1)
register()
v-model
state, message
Checkbox
register()
v-model:checked
invalid
CheckboxWithLabel
register()
v-model:checked
errorText
DropdownField
Controller: value, onValueChange
v-model
state, errorText
OtpInput
Controller: value, onValueChange
v-model
error, errorMessage
DatePicker
Controller: value, onValueChange
v-model (or v-model:range)
none: show the message beside it
Slider
Controller: value, onValueChange
v-model
none
Switch, SwitchWithLabel
Controller: checked, onCheckedChange
v-model:checked
none
SegmentedControl
Controller: value, onValueChange
v-model
none

For a field bound through Controller, also pass field.onBlur as onBlur. It fires once when focus leaves the whole field, not when it moves into the field's own popover, so touched state stays right.

Without a form library#

Give each field a name and it submits with a plain <form>, custom widgets included: a dropdown submits its value through a hidden input.

React
<form
onSubmit={(e) => {
e.preventDefault();
const data = Object.fromEntries(new FormData(e.currentTarget));
// { email: "ada@example.com", role: "eng", subscribe: "on" }
}}
>
<Input label="Email" name="email" required />
<DropdownField label="Role" name="role" required>
<SelectOption value="eng" label="Engineer" />
</DropdownField>
<CheckboxWithLabel label="Subscribe" name="subscribe" />
<FlatButton type="submit" label="Send" />
</form>

Accessibility#

  • Every field has a visible label. Use label, not a placeholder, to name a field.
  • Errors are text, not colour alone: the message sits under the field and is read with it.
  • Validate on submit first. Validating on every keystroke tells people they are wrong before they finish.
  • After a failed submit, move focus to the first field with an error. React Hook Form does this by default.