Skip to content

Formisch

Build forms in React using Formisch and Valibot.

This guide will cover building forms using the Field component, adding schema validation with Valibot, handling errors, ensuring accessibility, and more.

Demo

We’ll build a form with a text input and textarea. When you submit, the form data is validated and any errors will be shown.

Browser validation disabled

For the purposes of this demo, browser validation is disabled to illustrate schema validation. In production, keep native validation enabled when appropriate.

Bug Report
Help us improve by reporting bugs you encounter.
0/100 characters

Include steps to reproduce, expected behavior, and what actually happened.

Approach

This form uses Formisch for state and Valibot for validation. We'll build forms using the Field component, which gives you complete flexibility over the markup and styling.

  • Uses Formisch's useForm hook for form state management.
  • Uses the Form component for submit handling.
  • Import Formisch’s Field under an alias (FormischField) for controlled inputs.
  • Uses Shark Field components for building accessible forms.
  • Uses client-side validation by passing your Valibot schema into schema.

Anatomy

Typical structure: wrap each field with FormischField, and the Field component.

Form

Create a schema

Define your form shape with a Valibot schema.

Note: Formisch only supports Valibot for validation.

Setup

  • Create the form with useForm from Formisch and pass your schema to the schema option,
  • Wrap fields in Form and pass the form to the onSubmit option.
form.tsx

Build

Build the form using the FormischField from Formisch and the Shark Field.

Done

That's it. You now have a fully accessible form with client-side validation.

When you submit the form, the onSubmit handler on Form receives validated output. If the form data is invalid, Formisch will display the errors on field.errors for FieldError.

Validation

Client-side

Formisch validates your form data using the Valibot schema. Define a schema and pass it to the schema option of the useForm hook.

example-form.tsx

Modes

Configure when validation runs via the validate and revalidate options:

form.tsx

Displaying Errors

Display errors next to the field using FieldError. For styling and accessibility:

  • Add the invalid prop to the Field component.
  • Don't need to add the invalid prop to the form control such as Input, Checkbox, etc.
form.tsx

Different types of fields

Input

  • Spread field.props on Input and set value={field.input}.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Profile Settings
Update your profile information below.

This is your public display name. Must be between 3 and 10 characters. Must only contain letters, numbers, and underscores.

Textarea

  • Spread field.props on Textarea and set value={field.input}.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Personalization
Customize your experience by telling us more about yourself.

Tell us more about yourself. This will be used to help us personalize your experience.

NativeSelect

  • Spread field.props on NativeSelect and set value={field.input}.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Language Preferences
Select your preferred spoken language.

For best results, select the language you speak.

Select

  • Wire field.input and field.onChange to Select.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Language Preferences
Select your preferred spoken language.

For best results, select the language you speak.

Checkbox

  • Wire field.input and field.onChange to Checkbox.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
  • Remember to add data-slot="checkbox-group" to the FieldGroup component for proper styling and spacing.
Notifications
Manage your notification preferences.
Responses

Get notified for requests that take time, like research or image generation.

Tasks

Get notified when tasks you've created have updates.

Radio group

  • Wire field.input and field.onChange to RadioGroup.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Subscription Plan
See pricing and features for each plan.
Plan

You can upgrade or downgrade your plan at any time.

Switch

  • Wire field.input and field.onChange to Switch.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Security Settings
Manage your account security preferences.

Enable multi-factor authentication to secure your account.

NumberInput

  • Wire field.input and field.onChange to NumberInput.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Salary expectations
Share your target gross annual salary in EUR.
EUR

Slider

  • Wire field.input and field.onChange to Slider.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Listings filter
Set a price range so we only show results in your budget.

Drag each thumb to set the lower and upper bound.

Combobox

  • Wire field.input and field.onChange to Combobox.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Role setup
Tell us which team you work with most so we can tailor workflows.

Type to filter the list, then pick one option.

Autocomplete

  • Wire field.input and field.onChange to Autocomplete.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Tech stack
Select the technology you're most familiar with.

Type to filter the list, then pick one option.

Date Picker

  • Wire field.input and field.onChange to DatePicker.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Scheduling
Pick a day that works for a first-round interview. We will confirm by email.

Input OTP

  • Wire field.input and field.onChange to InputOTP.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Account recovery
Enter one of your single-use backup codes if you cannot access your authenticator app.

Codes can only be used once.

Rating

  • Use field.onChange with Rating’s onValueChange and bind value from field.input.
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Quick feedback
Help us improve by sharing how likely you are to recommend Shark UI to a colleague.

1 = not likely, 5 = very likely.

File Upload

  • Use FileUpload onFileAccept to push files into the field value (field.onChange).
  • Add the invalid prop to the Field component and pass the error message to the FieldError component.
Application materials
Upload your résumé or CV.

Accepted formats: PDF and Word documents.

Complex Forms

Here is an example of a more complex form with multiple fields and validation.

You're almost there!
Choose your subscription plan and billing period.
Subscription plan

Choose your subscription plan.

Choose how often you want to be billed.

Add-ons

Select additional features you'd like to include.

Advanced analytics and reporting

Automated daily backups

24/7 premium customer support

Receive email updates about your subscription.

Resetting the Form

Import reset and pass the form to reset the form to its default values.

Array Fields

Formisch provides a FieldArray component for managing dynamic array fields. Also provides helpers like insert and remove for dynamic lists. This is useful when you need to add or remove fields dynamically.

Contact emails
Manage your contact email addresses.
Email addresses

Add up to 5 email addresses where we can contact you.

Array Field Structure

Use FieldArray component and pass the form to the of prop.

form.tsx

Nested Fields

Use arrayField.items to render each nested field, passing the correct path for each item.

form.tsx

Adding items

  • Use insert to add a new array item.
  • Provide initialInput matching the nested structure of the array element.
form.tsx

Removing items

Use remove with the array path and index.

form.tsx

Array validation

Use Valibot's array method to validate array fields.

form.tsx