Form
A neo-brutalist style form system built on vee-validate, providing composable sub-components for structured form layout.
Demo
Installation
pnpm dlx brutx-vue@latest add formForm requires vee-validate@^4.15.1. The examples below also use the Zod adapter.
pnpm add 'vee-validate@^4.15.1' '@vee-validate/zod@^4.15.1' 'zod@^3.24.0'Usage
Changing field values or starting a new form validation invalidates a submission that is awaiting asynchronous validation. A submission emits submit only if it passes validation while still current.
The instance method validateField(path) and vee-validate's form-context field validation use the same rules. The schema can validate paths without a separately registered field, such as users[0].email managed by useFieldArray('users'). The vee-validate context method returns { valid, errors }, and the Form instance returns valid.
<script setup>
import { toTypedSchema } from '@vee-validate/zod'
import { z } from 'zod'
import { Form, FormField, FormItem, FormLabel, FormControl, FormDescription, FormMessage } from 'brutx-ui-vue/form'
import { Input, Button } from 'brutx-ui-vue'
const schema = toTypedSchema(z.object({
username: z.string().min(2).max(50),
email: z.string().email(),
}))
function onSubmit(values) {
console.log('Form submitted:', values)
}
</script>
<template>
<Form :validation-schema="schema" @submit="onSubmit">
<FormField name="username" v-slot="{ componentField }">
<FormItem>
<FormLabel>Username</FormLabel>
<FormControl>
<Input v-bind="componentField" placeholder="Enter username" />
</FormControl>
<FormDescription>This is your public display name.</FormDescription>
<FormMessage />
</FormItem>
</FormField>
<FormField name="email" v-slot="{ componentField }">
<FormItem>
<FormLabel>Email</FormLabel>
<FormControl>
<Input v-bind="componentField" type="email" placeholder="you@example.com" />
</FormControl>
<FormMessage />
</FormItem>
</FormField>
<Button type="submit" variant="primary">Submit</Button>
</Form>
</template>FormWizard Multi-step Form
A wizard-style form built on the Stepper component, supporting step validation, linear navigation, and custom step content.
When steps are reordered, the wizard preserves the active step, completion status, and validation errors by step.id. Adding, replacing, or removing a step's validator clears that step's validation errors and completion status. If the active step is removed, the wizard selects the step at its former position or the last step if that position is out of range.
<script setup>
import { ref } from 'vue'
import { FormWizard } from 'brutx-ui-vue/form'
import type { FormStep } from 'brutx-ui-vue/form'
import { Input } from 'brutx-ui-vue'
const values = ref({})
const steps = [
{ id: 'personal', title: 'Personal Info' },
{ id: 'address', title: 'Address' },
{ id: 'review', title: 'Review' },
]
function onComplete(finalValues) {
console.log('Submitted:', finalValues)
}
</script>
<template>
<FormWizard
v-model="values"
:steps="steps"
@complete="onComplete"
>
<template #step-personal>
<Input v-model="values.name" placeholder="Name" />
</template>
<template #step-address>
<Input v-model="values.address" placeholder="Address" />
</template>
<template #step-review>
<p>Review info: {{ values }}</p>
</template>
</FormWizard>
</template>FormConditional Conditional Fields
The predicate receives the current form values. This example uses a field named value. Enter company or personal to display the corresponding field group.
<script setup>
import { Input } from 'brutx-ui-vue'
import { Form, FormConditional, FormField } from 'brutx-ui-vue/form'
const initialValues = { value: 'company' }
const showCompany = (values) => values.value === 'company'
const showPersonal = (values) => values.value === 'personal'
</script>
<template>
<Form :initial-values="initialValues">
<FormField name="value" v-slot="{ componentField }">
<Input v-bind="componentField" aria-label="Account type" />
</FormField>
<FormConditional :when="showCompany">
<FormField name="companyName" v-slot="{ componentField }">
<Input v-bind="componentField" aria-label="Company name" />
</FormField>
</FormConditional>
<FormConditional :when="showPersonal">
<FormField name="idNumber" v-slot="{ componentField }">
<Input v-bind="componentField" aria-label="Identification number" />
</FormField>
</FormConditional>
</Form>
</template>Binding FormControl to an Input
FormControl requires exactly one root control. Its scoped slot provides the generated ID, description and invalid-state attributes, and the shared form size:
<FormControl v-slot="{ id, ariaDescribedby, ariaInvalid, size }">
<Input :id="id" :size="size" :aria-describedby="ariaDescribedby" :aria-invalid="ariaInvalid" />
</FormControl>Sub-components
| Component | Description |
|---|---|
Form | Root form component, integrates with vee-validate |
FormField | Field wrapper that connects form state and provides field context |
FormItem | Layout container for label, control, and message; generates unique IDs and applies form layout settings |
FormLabel | Label with error state support, injects field context |
FormControl | Input control wrapper that places the control in the form grid and provides accessibility attributes and size via slot |
FormDescription | Helper text below the input |
FormMessage | Validation error message, injects field context |
FormWizard | Multi-step wizard-style form |
FormConditional | Dynamically shows/hides field groups based on form values |
Data Types
FormFieldContext
FormField provides FormFieldContext to child components via provide/inject:
interface FormFieldContext {
name: ComputedRef<string>
error: Ref<string | undefined>
value: Ref<unknown>
setValue: (value: unknown) => void
setError: (message: string | undefined) => void
}FormItemContext
FormItem provides FormItemContext to child components via provide/inject:
interface FormItemContext {
id: string
formItemId: string
formDescriptionId: string
formMessageId: string
}FormControl, FormLabel, and FormMessage all inject FormFieldContext and FormItemContext, providing direct access to field values and error states.
FormStep
interface FormStep {
id: string
title: string
description?: string
icon?: Component
validator?: (values: Record<string, unknown>) => ValidationResult
optional?: boolean
}ValidationResult
interface ValidationResult {
valid: boolean
errors: Record<string, string>
}Composables
useFormWizard
Access the wizard context within FormWizard child components:
const {
currentStep, // Ref<number> - Current step, writable to request navigation
steps, // ComputedRef<FormStep[]> - Step configuration
values, // ComputedRef<Record<string, unknown>> - Form data
updateValues, // (values: Record<string, unknown>) => void - Update form data
nextStep, // () => void - Go to next step
previousStep, // () => void - Go to previous step
goToStep, // (step: number) => void - Jump to a step
complete, // () => void - Complete the form
getStepErrors, // (step: number) => Record<string, string> | undefined - Get errors for a specific step
isFirstStep, // ComputedRef<boolean>
isLastStep, // ComputedRef<boolean>
canGoNext, // ComputedRef<boolean>
} = useFormWizard()You can call updateValues() several times in one interaction. The wizard emits the first update immediately, then merges each subsequent patch into the modelValue accepted by the parent. nextStep(), a forward jump in linear mode, and complete() wait for the pending updates and validate the values accepted by the parent. If the parent waits for a remote response to update modelValue, request navigation or submission after the parent accepts the change.
Assigning to currentStep.value follows the same rules as calling goToStep() or using the step indicator. In linear mode, forward jumps require all required steps before the target to be complete. Next completes the current step. With validateOnNext enabled, the wizard revalidates required preceding steps against the latest shared values. If a prerequisite is incomplete or fails validation, the wizard stays on the current step and emits navigation-blocked. Read validation errors with getStepErrors(). Each step transition emits step-change once.
Programmatic Control
Access the Form component instance via ref to call the following methods:
<script setup>
import { ref } from 'vue'
import { Form } from 'brutx-ui-vue/form'
const formRef = ref(null)
async function handleSubmit() {
const isValid = await formRef.value?.validate()
if (isValid) {
// Submit form
}
}
function handleReset() {
formRef.value?.resetFields()
}
</script>
<template>
<Form ref="formRef" :scroll-to-error="true">
<!-- Form fields -->
</Form>
</template>clearValidate() and clearValidate([]) clear all field errors. clearValidate(['email']) clears only the named field's error. Field values are preserved.
scrollToField('email') locates a visible control by its FormField path. With scrollToError enabled, an unsuccessful validate() scrolls to the first visible invalid control.
Native reset buttons and HTMLFormElement.reset() use the resetFields() flow when the reset event is allowed. The reset restores initial field values and clears validation state. Wait for the view to update before reading FormData if you need the updated native control values.
Call validate(), validateField(), or submit the form as soon as reset() returns. Submission handlers created with the injected handleSubmit also use the restored field values. Silent validation after a reset updates field validity, and explicit validation displays errors. Match each custom native control's name to its registered field path so the native form and field model use the same initial value.
Form submissions reflect initialValues and field updates. If a NumberInput field has no form-level initial value, resetting restores the field model to undefined and the control to its own defaultValue. Set the field in initialValues to share the same initial value across the form model, control, and native submission.
API Reference
Form
| Name | Type | Default | Description |
|---|---|---|---|
inline | boolean | undefined | false | Arranges form items in a wrapping horizontal layout. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/form/Form.vue | |||
labelPosition | "right" | "left" | "top" | undefined | "right" | left and right place labels to the left of controls with the corresponding alignment; top places labels above controls. |
DetailsDeclared type"right" | "left" | "top" | undefined Literal values
Default value details
Sourcepackages/ui/src/components/form/Form.vue | |||
labelWidth | string | number | undefined | undefined | The label-column width in horizontal label layouts. Numbers are pixels and strings are CSS widths; top layouts do not use this value. |
DetailsDeclared typestring | number | undefined Default value details
Sourcepackages/ui/src/components/form/Form.vue | |||
scrollToError | boolean | undefined | false | Smoothly scrolls to the first visible invalid control when the instance validate() method fails. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/form/Form.vue | |||
size | "default" | "lg" | "sm" | undefined | "default" | Provides a shared size for item spacing, labels, descriptions, and errors, and exposes size to controls through the FormControl slot. |
DetailsDeclared type"default" | "lg" | "sm" | undefined Literal values
Default value details
Sourcepackages/ui/src/components/form/Form.vue | |||
class | string | undefined | undefined | Custom CSS classes merged onto the native form root. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/form/Form.vue | |||
initialValues | Record<string, unknown> | undefined | undefined | Initial field values passed to vee-validate. A changed prop reference resets the form with the new values while the form is pristine. |
DetailsDeclared typeRecord<string, unknown> | undefined Default value details
Sourcepackages/ui/src/components/form/Form.vue | |||
validationSchema | unknown | undefined | A vee-validate schema, such as a field rule map or a converted Zod schema. Set, replace, or remove the schema after mounting while preserving field values. When present, the schema determines validation rules. Removing it restores rules registered on individual fields. |
DetailsDeclared typeunknown Default value details
Sourcepackages/ui/src/components/form/Form.vue | |||
| Name | Signature or type | Description |
|---|---|---|
@submit | [values: Record<string, unknown>] | Emits field values after form submission passes vee-validate validation. |
DetailsDeclared type[values: Record<string, unknown>] Sourcepackages/ui/src/components/form/Form.vue | ||
| Name | Signature or type | Description |
|---|---|---|
#default | {} | Form fields, descriptions, and submission controls. Descendants can access the validation and layout context provided by Form. |
DetailsDeclared type{}Sourcepackages/ui/src/components/form/Form.vue | ||
| Name | Signature or type | Description |
|---|---|---|
validate | () => Promise<boolean> | Validates all fields and asynchronously returns their validity. On failure, scrolls to the first visible invalid control when scrollToError is enabled. |
DetailsDeclared type() => Promise<boolean> Sourcepackages/ui/src/components/form/Form.vue | ||
validateField | (field: string) => Promise<boolean> | Validates one field by path, including paths defined only in the schema, and asynchronously returns whether it is valid. |
DetailsDeclared type(field: string) => Promise<boolean> Sourcepackages/ui/src/components/form/Form.vue | ||
resetFields | () => void | Restores field values and validation state to their initial state. Native reset buttons and HTMLFormElement.reset() use the same flow when the reset event is allowed. |
DetailsDeclared type() => void Sourcepackages/ui/src/components/form/Form.vue | ||
clearValidate | (fields?: string[] | undefined) => void | Clears validation errors for the specified fields. Omission or an empty array clears all errors while preserving current field values. |
DetailsDeclared type(fields?: string[] | undefined) => void Sourcepackages/ui/src/components/form/Form.vue | ||
scrollToField | (field: string) => void | Finds a visible control associated with the field path through FormControl or a native control with a matching name, then smoothly scrolls to its top. Radio groups use the first visible control. |
DetailsDeclared type(field: string) => void Sourcepackages/ui/src/components/form/Form.vue | ||
FormConditional
| Name | Type | Default | Description |
|---|---|---|---|
whenRequired | (values: Record<string, unknown>) => boolean | — | Receives all current form values and returns whether to mount the conditional content. Use it inside Form and prefer a stable named function.
|
DetailsDeclared type(values: Record<string, unknown>) => boolean Default value details
Sourcepackages/ui/src/components/form/FormConditional.vue | |||
class | string | undefined | undefined | CSS classes merged onto the content wrapper div when the condition is true. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/form/FormConditional.vue | |||
| Name | Signature or type | Description |
|---|---|---|
#default | {} | Fields or other content mounted when the condition is true. The entire region is unmounted when it becomes false. |
DetailsDeclared type{}Sourcepackages/ui/src/components/form/FormConditional.vue | ||
FormControl
| Name | Type | Default | Description |
|---|---|---|---|
class | string | undefined | — | CSS classes merged with form-control layout styles and applied through as-child to the single root control in the default slot. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/form/FormControl.vue | |||
| Name | Signature or type | Description |
|---|---|---|
#default | { ariaDescribedby: string | undefined; ariaInvalid: true | undefined; size: FormLayoutSize | undefined; class: string; id: string | undefined; } | Contains one root control. Provides the FormItem-linked id, class, ariaDescribedby, ariaInvalid, and Form size for explicit binding. The description relationship includes the error message when meaningful error text is present. |
DetailsDeclared type{ ariaDescribedby: string | undefined; ariaInvalid: true | undefined; size: FormLayoutSize | undefined; class: string; id: string | undefined; }Referenced types FormLayoutSize Type definition — FormLayoutSizeexport type FormLayoutSize = 'sm' | 'default' | 'lg' Sourcepackages/ui/src/components/form/FormControl.vue | ||
FormDescription
| Name | Type | Default | Description |
|---|---|---|---|
class | string | undefined | — | CSS classes merged onto the field description paragraph, which uses the description ID provided by FormItem. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/form/FormDescription.vue | |||
| Name | Signature or type | Description |
|---|---|---|
#default | {} | Supporting guidance for the field. The description paragraph is not rendered when this slot is omitted. |
DetailsDeclared type{}Sourcepackages/ui/src/components/form/FormDescription.vue | ||
FormField
| Name | Type | Default | Description |
|---|---|---|---|
nameRequired | string | — | The field name registered with vee-validate. Keep it stable during the component lifetime; changing it requires handling retained values and errors for the previous name. |
DetailsDeclared typestring Default value details
Sourcepackages/ui/src/components/form/FormField.vue | |||
| Name | Signature or type | Description |
|---|---|---|
#default | { componentField: { name: string; modelValue: unknown; 'onUpdate:modelValue': (value: unknown, shouldValidate?: boolean) => void; onFocusout: (event: FocusEvent) => void; }; } | Field content, typically FormItem. Provides componentField for v-bind on the control, including the field name, current value, value updates, and focusout validation. |
DetailsDeclared type{ componentField: { name: string; modelValue: unknown; 'onUpdate:modelValue': (value: unknown, shouldValidate?: boolean) => void; onFocusout: (event: FocusEvent) => void; }; }Sourcepackages/ui/src/components/form/FormField.vue | ||
FormItem
| Name | Type | Default | Description |
|---|---|---|---|
class | string | undefined | — | CSS classes merged onto the field layout container, which follows the Form label position, width, and size. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/form/FormItem.vue | |||
| Name | Signature or type | Description |
|---|---|---|
#default | {} | The field label, control, description, and error message, sharing the IDs generated by this FormItem. |
DetailsDeclared type{}Sourcepackages/ui/src/components/form/FormItem.vue | ||
FormLabel
| Name | Type | Default | Description |
|---|---|---|---|
class | string | undefined | — | CSS classes merged onto the field label. Meaningful error text automatically applies the error color. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/form/FormLabel.vue | |||
| Name | Signature or type | Description |
|---|---|---|
#default | {} | The field label content, linked through for to the FormItem control ID. |
DetailsDeclared type{}Sourcepackages/ui/src/components/form/FormLabel.vue | ||
FormMessage
| Name | Type | Default | Description |
|---|---|---|---|
class | string | undefined | — | CSS classes merged onto the validation error paragraph. It renders with role="alert" only when the field error is non-empty after trimming. |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/form/FormMessage.vue | |||
FormWizard
| Name | Type | Default | Description |
|---|---|---|---|
stepsRequired | FormStep[] | — | The step configuration list with stable IDs, titles, and optional validators. Each ID determines its step-{id} slot name. Changing a validator clears that step’s errors and completion status. |
DetailsDeclared typeFormStep[] Referenced types FormStep Type definition — FormStepexport interface FormStep {
/** 向导内唯一且稳定的步骤标识 */
id: string
title: string
description?: string
icon?: Component
/** 入参为全量 values(跨步骤共享的扁平字段集合),约定只返回当前步骤相关字段的错误 */
validator?: (values: Record<string, unknown>) => ValidationResult
/** 可直接跨过的线性前置步骤,通过下一步离开和完成表单时仍执行已启用的校验 */
optional?: boolean
}Default value details
Sourcepackages/ui/src/components/form/form-wizard-types.ts | |||
modelValue | Record<string, unknown> | undefined | { }Factory evaluated per instance | The flat field record shared across steps and controlled through v-model. useFormWizard().updateValues() emits updates after shallowly merging field patches. |
DetailsDeclared typeRecord<string, unknown> | undefined Default value details
Sourcepackages/ui/src/components/form/form-wizard-types.ts | |||
initialStep | number | undefined | 0 | The initial zero-based step index, clamped to the valid steps range at creation. |
DetailsDeclared typenumber | undefined Default value details
Sourcepackages/ui/src/components/form/form-wizard-types.ts | |||
validateOnNext | boolean | undefined | true | Uses parent-accepted values to validate the current step on Next, revalidate required preceding steps during linear advancement, and validate every step with a validator on completion. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/form/form-wizard-types.ts | |||
showIndicator | boolean | undefined | true | Shows the top step indicator, which users can use to request navigation. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/form/form-wizard-types.ts | |||
linear | boolean | undefined | true | Forward navigation requires all preceding required steps to be complete and pass any enabled validation. Optional steps can be skipped, and backward navigation is unrestricted. |
DetailsDeclared typeboolean | undefined Literal values
Default value details
Sourcepackages/ui/src/components/form/form-wizard-types.ts | |||
class | string | undefined | undefined | CSS classes merged onto the wizard root container with role="form". |
DetailsDeclared typestring | undefined Default value details
Sourcepackages/ui/src/components/form/form-wizard-types.ts | |||
| Name | Signature or type | Description |
|---|---|---|
@update:modelValue | [values: Record<string, unknown>] | Emits the complete field record after shallowly merging a field update made through the wizard context. |
DetailsDeclared type[values: Record<string, unknown>] Sourcepackages/ui/src/components/form/FormWizard.vue | ||
@complete | [values: Record<string, unknown>] | Emits all form data when the completion action passes the enabled step validation. |
DetailsDeclared type[values: Record<string, unknown>] Sourcepackages/ui/src/components/form/FormWizard.vue | ||
@step-change | [step: number, previousStep: number] | Emits once when the active step changes, with the target and previous zero-based indices. |
DetailsDeclared type[step: number, previousStep: number] Sourcepackages/ui/src/components/form/FormWizard.vue | ||
@validation-error | [step: number, errors: Record<string, string>] | Emits the step index and field error record when a step validator returns an invalid result. |
DetailsDeclared type[step: number, errors: Record<string, string>] Sourcepackages/ui/src/components/form/FormWizard.vue | ||
@navigation-blocked | [targetStep: number, blockedStep: number] | Emits the target and blocking indices when linear advancement encounters a required preceding step that is incomplete or fails validation. |
| Name | Signature or type | Description |
|---|---|---|
#step-{id} | { step: FormStep; index: number } | A dynamic slot named after the step ID, receiving its configuration and zero-based index. All step content remains mounted, with v-show selecting the current panel. |
DetailsDeclared type{ step: FormStep; index: number }Sourcepackages/ui/src/components/form/FormWizard.vue | ||
Accessibility
Form Structure
FormItemautomatically generates unique IDs, ensuring correct associations betweenFormLabel,FormControl,FormDescription, andFormMessage.FormLabelis associated with the corresponding input control via theforattribute.FormControlprovidesid,aria-describedby, andaria-invalidattributes via a scoped slot, which should be bound to the inner input control to ensure accessibility.
Validation Errors
- On validation failure,
FormLabelautomatically adds thetext-brutal-destructivestyle. FormMessageusesrole="alert"to ensure screen readers announce error messages promptly.- The
aria-invalidattribute on the input control is automatically set totrue.
FormWizard Navigation
- The step indicator uses a list and buttons. The current step is marked with
aria-current="step". - Supports keyboard navigation (Tab, Enter, Space).
FAQ
Q: Form validation is not triggered on submit. What's wrong?
A: Make sure you have correctly installed and configured the vee-validate and @vee-validate/zod dependencies, and that validationSchema is passed to the Form component. Additionally, each field that needs validation must be wrapped in a FormField, and the FormField's name attribute must match the field name in the schema.
Q: How do I skip validation for certain steps in FormWizard?
A: Set optional: true to allow linear navigation to skip the step. With validateOnNext enabled, clicking Next on that step or completing the form still runs its validator. To skip validation conditionally, have the validator return { valid: true, errors: {} } based on the current shared values.
Q: Why isn't the FormConditional condition function working?
A: Read fields directly from the when argument, for example values.type. If the field is named value, values.value is that field's value. Check the field names and supply initial values via initialValues. The predicate can also handle an undefined field value explicitly.