String
Input
Basic input field, configure icons, colors and other attributes via controlProps:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
text: afz.string({
controlProps: {
icon: 'i-lucide-text'
}
})
.min(3, '至少 3 个字符')
.max(20, '最多 20 个字符')
.meta({
label: '基础输入框',
placeholder: '请输入文本',
hint: '支持图标、颜色等配置'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Textarea
Multi-line text input, set type: 'textarea', supports auto height adjustment:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
description: afz.string({
type: 'textarea',
controlProps: {
maxrows: 2,
autoresize: true
}
})
.min(10, '至少 10 个字符')
.meta({
label: '文本域',
placeholder: '请输入多行文本...',
hint: '这是一个长文本,将自动调整高度,最多 2 行。'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
PinInput
Verification code or PIN input, set type: 'pinInput':
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
code: afz.string({
type: 'pinInput',
controlProps: {
length: 6,
mask: false
}
})
.length(6, '请输入 6 位验证码')
.meta({
label: '验证码输入',
hint: '输入 6 位验证码'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
InputMenu
Supports both typing and dropdown selection, set type: 'inputMenu':
<script lang="ts" setup>
import type { FormSubmitEvent, InputMenuItem } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const items = [
{ label: 'Apple', value: 'apple' },
{ label: 'Banana', value: 'banana' },
{ label: 'Orange', value: 'orange' },
{ label: 'Grape', value: 'grape' }
] satisfies InputMenuItem[]
const schema = afz.object({
fruit: afz.string({
type: 'inputMenu',
controlProps: {
items,
valueKey: 'value'
}
})
.meta({
label: '输入菜单',
placeholder: '输入或选择水果',
hint: '支持输入和下拉选择'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Dynamic Type
Switch between input and textarea in an interactive example, and adjust size, color, rows and max length.
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type z from 'zod'
const props = defineProps<{
type: 'string' | 'textarea'
size: 'sm' | 'xs' | 'md' | 'lg' | 'xl'
color: 'error' | 'primary' | 'secondary' | 'success' | 'info' | 'warning' | 'neutral'
rows?: number
maxLength?: number
}>()
const { afz } = useAutoForm()
const toast = useToast()
const resolvedControlProps = computed(() => ({
leadingIcon: 'i-lucide-type',
color: props.color,
rows: props.rows,
maxlength: props.maxLength
}))
const schema = computed(() => (afz.object({
stringWithType: afz.string({
type: props.type,
controlProps: resolvedControlProps.value
}).meta({
size: props.size
}).optional()
})))
type Schema = z.output<typeof schema.value>
const form = ref<Partial<Schema>>({})
async function onSubmit(event: FormSubmitEvent<Schema>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<UCard class="w-lg">
<MAutoForm
v-model="form"
:schema="schema"
:submit-button-props="{
color
}"
@submit="onSubmit"
/>
</UCard>
</template>
Number
InputNumber
Basic number input, supports integer, decimal, range limits and other configurations:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
count: afz.number()
.int('必须是整数')
.min(0, '不能小于 0')
.max(100, '不能大于 100')
.meta({
label: '数字输入',
placeholder: '请输入 0-100 的整数',
hint: '支持步长、范围限制等配置'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Slider
Numeric slider, set type: 'slider', suitable for selecting values within a defined range:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
volume: afz.number({
type: 'slider',
controlProps: {
min: 0,
max: 100,
step: 5
}
})
.default(50)
.meta({
label: '音量滑块',
hint: '拖动滑块调整数值,步长为 5'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
InputRating
Rating input, pass the UInputRating component instance directly via component, supports half-star granularity and customizable star count:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
import { UInputRating } from '#components'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
rating: afz.number({
component: UInputRating,
controlProps: {
step: 0.5,
length: 10
}
})
.min(0, '评分不能小于 0')
.max(10, '评分不能大于 10')
.meta({
label: '评分',
hint: '支持半星评分'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Boolean
Checkbox
Default checkbox control, suitable for single option selection:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
agree: afz.boolean({
controlProps: {
label: '我已阅读并同意服务条款'
}
})
.refine(val => val === true, '必须同意条款才能继续')
.meta({
label: '复选框'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Switch
Toggle switch, set type: 'switch', suitable for enabling/disabling features:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
enabled: afz.boolean({
type: 'switch'
})
.default(true)
.meta({
label: '开关',
hint: '切换功能启用/禁用状态'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Toggle
Use afz.boolean({ type: 'toggle' }) for a pressable button that represents a boolean, such as pinning a post. Set the button text with controlProps.label.
<script setup lang="ts">
import type { z } from 'zod'
const { afz } = useAutoForm()
const schema = afz.object({
pinned: afz.boolean({
type: 'toggle',
controlProps: { label: '置顶', activeVariant: 'solid' }
}).default(false).meta({ label: '文章设置' })
})
const state = reactive<z.input<typeof schema>>({})
</script>
<template>
<MAutoForm v-model="state" :schema="schema" :submit="false" />
</template>
Date & Time
Use afz.calendarDate() to create a date field, powered by @internationalized/date for robust date handling:
Date Picker
Single date selection with date formatting and validation:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
import { CalendarDate } from '@internationalized/date'
const { afz } = useAutoForm()
const toast = useToast()
const formatter = useDateFormatter()
const schema = afz.object({
appointmentDate: afz.calendarDate({ controlProps: { labelFormat: 'iso' } })
.refine(
date => date > new CalendarDate(2026, 1, 1),
{ message: '日期必须在 2026 年之后' }
)
.transform(date => formatter.toTimestamp(date))
.meta({ label: '预约日期', description: '请选择一个在 2026 年之后的日期' })
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Date Range
Date range selection, set controlProps.range: true:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
vacation: afz.calendarDate({
controlProps: {
labelFormat: 'iso',
range: true,
valueFormat: 'iso',
numberOfMonths: 2
}
})
.meta({
label: '日期范围',
hint: '选择日期范围,显示两个月'
})
})
type Schema = z.output<typeof schema>
async function onSubmit(event: FormSubmitEvent<Schema>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Date Input
Use afz.inputDate() to create a date input field, based on the UInputDate component for a native date input experience.
Single Date Input
A clean date input field, suitable for form filling:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const formatter = useDateFormatter()
const schema = afz.object({
birthDate: afz.inputDate()
.transform(date => formatter.convertToISO(date))
.meta({
label: '出生日期',
description: '使用日期输入框选择您的出生日期'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Date Range Input
A date input field with range selection support:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const formatter = useDateFormatter()
const schema = afz.object({
eventPeriod: afz.inputDate({
controlProps: {
range: true
}
})
.transform(date => formatter.convertToISO(date))
.meta({
label: '活动周期',
hint: '选择活动的起止日期'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Time Input
Use afz.inputTime() to create a time input field, returning the Time type (from @internationalized/date):
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
meetingTime: afz.inputTime()
.refine(
time => time.hour >= 9 && time.hour < 18,
{ message: '会议时间必须在工作时间内 (9:00-18:00)' }
)
.transform(time => `${String(time.hour).padStart(2, '0')}:${String(time.minute).padStart(2, '0')}`)
.meta({
label: '会议时间',
description: '请选择会议时间(工作时间内)'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
ISO Format Strings
For scenarios requiring ISO standard format strings, three factory methods are provided:
afz.isoDate()- validatesYYYY-MM-DDformatafz.isoTime()- validatesHH:MM[:SS[.s+]]formatafz.isoDatetime()- validates ISO 8601 full format
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
scheduledDate: afz.isoDate()
.meta({
label: '计划日期',
description: '输入 ISO 格式日期 (YYYY-MM-DD)',
hint: '例如: 2025-12-31'
}),
scheduledTime: afz.isoTime()
.meta({
label: '计划时间',
description: '输入 ISO 格式时间 (HH:MM:SS)',
hint: '例如: 14:30:00'
}),
createdAt: afz.isoDatetime()
.optional()
.meta({
label: '创建时间',
description: '输入 ISO 8601 完整时间戳',
hint: '例如: 2025-12-31T14:30:00Z'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Enum
Without specifying a control type, uses the default select control:
afz.enum(['a', 'b'], {
label: 'Select Option',
hint: 'Please choose one'
})
Override the default control via type, while keeping the output type unchanged:
afz.enum(['a', 'b', 'c'], {
type: 'radioGroup', // overrides the default select
controlProps: {
items: [
{ label: 'Option A', value: 'a' },
{ label: 'Option B', value: 'b' }
]
}
})
// Output type is still: 'a' | 'b' | 'c'
// but rendered using radioGroup
Pass a custom component instance directly:
import MyEnumControl from './MyEnumControl.vue'
afz.enum(['a', 'b'], {
component: MyEnumControl,
controlProps: { /* ... */ }
})
controlProps.items takes the highest priority for rendered content, while the first argument of enum() determines the output type — both can be used together.- Method 1: Auto-extract enum values, the system automatically converts enum values to
items:const Fruits = ['apple', 'banana', 'orange'] as const const schema = afz.object({ fruit: afz.enum(Fruits, { type: 'selectMenu', controlProps: { placeholder: 'Select a fruit' // System auto-converts: items: ['apple', 'banana', 'orange'] } }) }) - Method 2: Provide items manually, use empty array
[]as the first argument, fully controlled byitemsfor type and rendering:const schema = afz.object({ fruit: afz.enum([], { type: 'selectMenu', controlProps: { valueKey: 'value', items: [ { label: 'Apple', value: 'apple' }, { label: 'Banana', value: 'banana' } ] } }) }) - Method 3: Type + custom display (recommended), use both the enum argument and
itemsfor type safety with custom display:const schema = afz.object({ gender: afz.enum(['male', 'female', 'other'], { type: 'radioGroup', controlProps: { items: [ { label: 'Male', value: 'male' }, { label: 'Female', value: 'female' }, { label: 'Other', value: 'other' } ] } }) }) // Output type: 'male' | 'female' | 'other' // Display: Male / Female / Other
Select
Native select box, suitable for simple enum values:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
status: afz.enum(['active', 'inactive', 'pending'])
.default('active')
.meta({
label: '选择框',
placeholder: '请选择状态',
hint: '基础枚举选择'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
SelectMenu
Dropdown menu, set type: 'selectMenu', supports search, icons, and grouping:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
fruit: afz.enum([], {
type: 'selectMenu',
controlProps: {
placeholder: '选择水果',
valueKey: 'value',
items: [
{ label: '苹果', value: 'apple' },
{ label: '香蕉', value: 'banana' },
{ label: '橙子', value: 'orange' }
]
}
})
.meta({
label: '下拉菜单'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
RadioGroup
Radio button group, set type: 'radioGroup', suitable for 2-4 options:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
theme: afz.enum([], {
type: 'radioGroup',
controlProps: {
valueKey: 'id',
items: [
{ label: 'System', description: '跟随系统设置', id: 'system' },
{ label: 'Light', description: '浅色主题', id: 'light' },
{ label: 'Dark', description: '深色主题', id: 'dark' }
]
}
})
.default('system')
.meta({
label: '单选组'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: JSON.stringify(event.data, null, 2)
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
ToggleGroup
Use afz.enum(values, { type: 'toggleGroup' }) for a single-select button group. AutoForm creates items from the enum values. Pass controlProps.items to use custom labels. Pressing the selected option again clears it. Add .optional() to the schema if the field can be left empty.
For multiple selection, use afz.array() with type: 'toggleGroup', then pass type: 'multiple' and an array of item objects in controlProps. The field value is an array of the selected items' value properties.
<script setup lang="ts">
import type { z } from 'zod'
const { afz } = useAutoForm()
const schema = afz.object({
period: afz.enum(['日', '周', '月'], {
type: 'toggleGroup',
controlProps: { attached: true }
}).optional().meta({ label: '统计周期' }),
formats: afz.array(afz.enum(['bold', 'italic', 'underline']), {
type: 'toggleGroup',
controlProps: {
type: 'multiple',
items: [
{ label: '加粗', value: 'bold' },
{ label: '斜体', value: 'italic' },
{ label: '下划线', value: 'underline' }
]
}
}).default([]).meta({ label: '文字格式' })
})
const state = reactive<z.input<typeof schema>>({})
</script>
<template>
<MAutoForm v-model="state" :schema="schema" :submit="false" />
</template>
File
afz.file() to create a file upload field:Single File Upload
Basic single file upload:
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
avatar: afz.file({
controlProps: {
accept: 'image/*'
}
})
.meta({
label: '单文件上传',
hint: '仅支持图片格式'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: `文件名: ${event.data.avatar.name}`
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>
Multiple File Upload
Multiple file upload using array with type: 'file':
<script lang="ts" setup>
import type { FormSubmitEvent } from '@nuxt/ui'
import type { z } from 'zod'
const { afz } = useAutoForm()
const toast = useToast()
const schema = afz.object({
attachments: afz.array(afz.file(), {
type: 'file',
controlProps: {
multiple: true,
layout: 'list'
}
})
.min(1, '至少上传一个文件')
.max(5, '最多上传 5 个文件')
.meta({
label: '多文件上传',
hint: '最多上传 5 个文件'
})
})
async function onSubmit(event: FormSubmitEvent<z.output<typeof schema>>) {
toast.add({
title: 'Success',
color: 'success',
description: `已上传 ${event.data.attachments.length} 个文件`
})
}
</script>
<template>
<MAutoForm :schema="schema" @submit="onSubmit" />
</template>