Form Primitive
Use Form to validate inputs before submitting them. It runs native constraints and your own validators, then lets you choose when to show errors.
Anatomy
Use the Astro primitive adapter to render Form anatomy with the Runtime wiring included.
---import { Form } from "@starwind-ui/astro/form";---
<Form.Root> <Form.ErrorSummary>Please fix the highlighted fields.</Form.ErrorSummary></Form.Root>Use the React primitive adapter when Form state participates in React rendering.
import { Form } from "@starwind-ui/react/form";
export function Example() { return ( <Form.Root> <Form.ErrorSummary>Please fix the highlighted fields.</Form.ErrorSummary> </Form.Root> );}Use the Vue components to build Form.
<script setup lang="ts">import Form from "@starwind-ui/vue/form";</script>
<template> <Form.Root> <Form.ErrorSummary>Please fix the highlighted fields.</Form.ErrorSummary> </Form.Root></template>Use the Svelte components to build Form.
<script lang="ts"> import { Form } from "@starwind-ui/svelte/form";</script>
<Form.Root> <Form.ErrorSummary>Please fix the highlighted fields.</Form.ErrorSummary></Form.Root>Render the Form data-sw-* contract yourself, then initialize createForm.
<form data-sw-form data-slot="form"> <div data-sw-form-error-summary data-slot="form-error-summary" role="status" aria-live="polite" aria-atomic="true" hidden>Please fix the highlighted fields.</div></form>
<script type="module"> import { createForm } from "@starwind-ui/runtime/form";
const root = document.querySelector("[data-sw-form]"); if (root) { createForm(root); }</script>Astro, React, Vue, and Svelte components use the same underlying interactions. Each framework has its own way to read and update state. See the examples and your installed package’s types for the available props and events. Plain HTML uses DOM events and Runtime methods.
Validation policy values
validationTiming, revalidationTiming, and errorVisibility accept semantic change, blur, submit, or manual causes. change means each accepted value revision for native and Runtime controls. Before submission, only validationTiming is active; after a submission attempt, revalidationTiming replaces it. The defaults are validationTiming="submit", revalidationTiming="change", and errorVisibility="submit".
API Reference
Root
The main element that owns the Form Runtime instance.
- Default element
- form
- Discovery hook
- data-sw-form
- Role
- -
Props
options React, Vue, Svelte only FormOptions -
- Description
- Configures the options option for the Root part.
- Kind
- option
- Targets
- root
- Frameworks
- React, Vue, Svelte
- Full type
- FormOptions
errors React, Vue, Svelte only FormExternalErrors -
- Description
- Configures the errors option for the Root part.
- Kind
- option
- Targets
- root
- Frameworks
- React, Vue, Svelte
- Full type
- FormExternalErrors
errorOptions React, Vue, Svelte only FormExternalErrorOptions -
- Description
- Configures the error options option for the Root part.
- Kind
- option
- Targets
- root
- Frameworks
- React, Vue, Svelte
- Full type
- FormExternalErrorOptions
data-error-visibility "blur" | "change" | "manual" | "submit" -
- Description
- Low-level errorVisibility form for semantic change, blur, submit, or manual validation; defaults to submit.
- Kind
- option
- Targets
- root
- Full type
- "blur" | "change" | "manual" | "submit"
data-revalidation-timing "blur" | "change" | "manual" | "submit" -
- Description
- Low-level revalidationTiming form that replaces validationTiming after a submission attempt; defaults to change.
- Kind
- option
- Targets
- root
- Full type
- "blur" | "change" | "manual" | "submit"
data-validation-timing "blur" | "change" | "manual" | "submit" -
- Description
- Low-level validationTiming form for semantic change, blur, submit, or manual validation before submission; defaults to submit.
- Kind
- option
- Targets
- root
- Full type
- "blur" | "change" | "manual" | "submit"
errorVisibility "blur" | "change" | "manual" | "submit" submit
- Description
- Selects whether semantic change, blur, submit, or manual validation reveals errors; defaults to submit.
- Kind
- option
- Targets
- root
- Full type
- "blur" | "change" | "manual" | "submit"
revalidationTiming "blur" | "change" | "manual" | "submit" change
- Description
- After a Form submission attempt, replaces validationTiming with semantic change, blur, submit, or manual validation; defaults to change.
- Kind
- option
- Targets
- root
- Full type
- "blur" | "change" | "manual" | "submit"
validationTiming "blur" | "change" | "manual" | "submit" submit
- Description
- Selects semantic change, blur, submit, or manual validation before a Form submission attempt; defaults to submit.
- Kind
- option
- Targets
- root
- Full type
- "blur" | "change" | "manual" | "submit"
Data Attributes
Runtime hooks
Metadata
Error Summary
Summary of validation errors for Form.
- Default element
- div
- Discovery hook
- data-sw-form-error-summary
- Role
- -
Data Attributes
Runtime hooks
Metadata
Runtime API
- Factory
createForm- Import
@starwind-ui/runtime/form- Root hook
- root
data-sw-form - Option props
- -
Related Styled Components
Changelog
View version history v1.1.3 9 releases
v1.1.3
- In Svelte, `Form.Root` prints its validation timing attributes only when you set the matching props, as in the other frameworks.
- In Vue, this component now needs Vue 3.5.21 or newer.
v1.1.2
- In Astro, an attribute that you set on a Form part now wins over the part's own default for the same attribute, such as `role` or `aria-label`.
v1.1.1
- A submit button with `formnovalidate`, such as "Save draft", now submits the form while required fields are still empty. Your `onSubmit` handler runs, and `submitter` is that button. If you do not use `onSubmit`, the browser submits the form. Other submit buttons still validate. A `novalidate` attribute on the form does not turn off Form validation.
- When a draft submit starts while a normal submit waits for an async check, only the draft submit runs, so `onSubmit` runs one time.
- Errors that you show with `validate()` now go away when the user fixes the field. This works for `validate({ names, reveal: true })` and for errors that you show with `errorVisibility="manual"`. These fields now check again on the `revalidationTiming` schedule, the same as after a submit. `resetValidation()` and a form reset put them back on the `validationTiming` schedule.
- An async field check, such as a username lookup, no longer gets lost when the user edits another field before the check finishes.
- When a required Select or Radio Group is the first field with an error, submit and `validate({ focus: true })` now put focus on the Select trigger or on a radio, and the page scrolls to it. Before, focus did not go to any control that the user could see.
v1.1.0
- Added React, Vue, and Svelte props for validation, server errors, and when errors appear. Supports asynchronous validators and settings for submission and form resets.
- Reduced repeated JavaScript when Astro Form is used with other Starwind components.
v1.0.1
- Removed old comments from the Vue Primitive files that said the code was internal and not for release. The CLI also removes the blank line that these comments left in the Vue Primitive `index.ts` files that it copies into your project.
v1.0.0
- This Primitive is now stable at version `1.0.0`. Its API and behavior stay the same as in the previous release.
v0.2.1
- The React Primitive files that the CLI copies into your project are now marked with `"use client"`, so they stay client components in React server frameworks.
v0.2.0
- Changed the validation timing options. `validationTiming` sets when fields check before the first submit, and `revalidationTiming` sets when they check after it. The two no longer add together. Form also has new methods to validate fields, show errors, reset validation, and set errors from outside the form, such as server errors.
- The old `input` timing and the old `change` timing both become `change`. The old `change` timing ran only after the user finished a change. The new `change` runs on every accepted change to the value.
v0.1.0
- First release. Form works with the native HTML form. It handles validation timing, the submit state, and field resets.