guides
Building Forms
Build forms with Field and Form, validate with native rules or Zod, show server errors, and connect React Hook Form or TanStack Form.
cnippet ui forms are built from two components:
- Field wraps one control with its label, description and error. It links them with the right
id,htmlForandaria-describedbyattributes and tracks whether the value is valid, touched or changed. - Form wraps the fields. It collects their values, runs validation on submit and shows server errors.
Every cnippet ui control works inside a Field: Input, Textarea, Select, Combobox, Checkbox, CheckboxGroup, RadioGroup, Switch, Slider, NumberField and OTPField.
A basic form
onFormSubmitreceives the values as an object keyed by each Field'sname, and it callspreventDefault()for you. It only runs when every field is valid.- Each Field needs a
name, or its value won't be invalues. - Button defaults to
type="button", so the submit button needstype="submit".
Validation
Native rules
Use HTML validation attributes on the control: required, type="email", minLength, maxLength, min, max and pattern. FieldError shows the browser's message when the value breaks a rule.
To write your own message, use match with the name of the rule:
The names come from the browser's ValidityState: valueMissing, typeMismatch, tooShort, tooLong, rangeUnderflow, rangeOverflow, patternMismatch and customError.
Custom rules
Pass a validate function to the Field. Return an error message, or null when the value is valid. It runs after the native rules pass, and it can be async:
The second argument of validate has the values of the whole form, which is useful for fields that depend on each other, such as "confirm password".
When validation runs
Set it on the Form for every field, or on a single Field to override it.
Validating with Zod
Validate the submitted values with a schema, then pass the messages to the Form's errors prop. Each message appears in the FieldError of the field with the same name:
An error in errors is cleared as soon as the user changes that field's value.
Server errors
Errors from your server work the same way. With a Server Function and useActionState, return an errors object and pass it to the Form:
React Hook Form
Use a Controller for each field and pass its state to the Field. invalid, touched and dirty let React Hook Form control what the Field shows. match={!!error} shows the FieldError whenever React Hook Form has an error for that field.
- Pass
handleSubmit(...)toonSubmit, notonFormSubmit. React Hook Form does the validation, so don't also addrequiredorvalidateto the Field. - Pass
field.refto the control so React Hook Form can focus the first invalid field. For Select, Combobox and NumberField, pass it to the part that renders the input (for Select, theinputRefprop). - Controls that pick a value (Select, Combobox, Checkbox, Switch, Slider, NumberField) take
valueandonValueChangein the same way.
TanStack Form
Use form.Field and pass its state to the cnippet Field. TanStack Form handles submission, so you don't need the cnippet Form component. Use a plain <form>.
With a Zod schema as the validator, errors holds issue objects, so read their message. With a function that returns a string, the errors are plain strings.
Grouping fields
Use Fieldset to group related fields under a legend, such as an address. For a group of checkboxes or radio buttons, wrap each option in FieldItem:
Good to know
- Don't add
id,htmlFororaria-describedbyyourself. Field links the label, description and error for you. FieldErroronly renders when the field is invalid, so you can leave it in place.- To wire a plain
<input>or another library's control to a Field, wrap it inFieldControl. - To style invalid controls, use the
data-invalidattribute that Field sets, for exampledata-invalid:border-destructive. The cnippet controls already have invalid styles. - The Form page has more examples: sign in, sign up, change password, multi-step onboarding and more.