FormField

GitHub
一个用于表单元素的包装器,提供验证和错误处理。

用法

使用 FormField 包装任何表单组件。在 Form 中使用时,它提供验证和错误处理。

标签

使用 label 属性设置表单控件的标签。

<template>
  <UFormField label="Email">
    <UInput placeholder="Enter your email" />
  </UFormField>
</template>
如果未提供,标签的 for 属性和表单控件将关联一个唯一的 id

使用 required 属性时,标签旁会添加一个星号。

<template>
  <UFormField label="Email" required>
    <UInput placeholder="Enter your email" />
  </UFormField>
</template>

描述

使用 description 属性在标签下方提供额外信息。

我们绝不会与任何人分享您的电子邮件。

<template>
  <UFormField label="Email" description="We'll never share your email with anyone else.">
    <UInput placeholder="Enter your email" class="w-full" />
  </UFormField>
</template>

提示

使用 hint 属性在标签旁边显示提示信息。

可选
<template>
  <UFormField label="Email" hint="Optional">
    <UInput placeholder="Enter your email" />
  </UFormField>
</template>

帮助

使用 help 属性在表单控件下方显示帮助信息。

请输入有效的电子邮件地址。
<template>
  <UFormField label="Email" help="Please enter a valid email address.">
    <UInput placeholder="Enter your email" class="w-full" />
  </UFormField>
</template>

错误

使用 error 属性在表单控件下方显示错误信息。当与 help 属性一起使用时,error 属性优先。

Form 内部使用时,当发生验证错误时会自动设置此项。

请输入有效的电子邮件地址。
<template>
  <UFormField label="Email" error="Please enter a valid email address.">
    <UInput placeholder="Enter your email" class="w-full" />
  </UFormField>
</template>
这会将表单控件的 color 设置为 error。您可以在 app.config.ts 中全局更改它。

尺寸

使用 size 属性更改 FormField 的大小,size 会代理到表单控件。

可选

我们绝不会与任何人分享您的电子邮件。

请输入有效的电子邮件地址。
<template>
  <UFormField
    label="Email"
    description="We'll never share your email with anyone else."
    hint="Optional"
    help="Please enter a valid email address."
    size="xl"
  >
    <UInput placeholder="Enter your email" class="w-full" />
  </UFormField>
</template>

API

属性

属性默认值类型
as'div'any

此组件应渲染为的元素或组件。

namestring

FormField 的名称。也用于匹配表单错误。

errorPatternRegExp

用于匹配表单错误名称的正则表达式。

labelstring
descriptionstring
帮助string
errorstring | false | true
提示string
尺寸'md'"md" | "xs" | "sm" | "lg" | "xl"
requiredboolean
eagerValidationboolean

如果为 true,则输入时会立即激活验证,而不是等待 blur 事件。

validateOnInputDelay`300`number

在输入事件上验证表单前的延迟(毫秒)。

ui{ root?: ClassNameValue; wrapper?: ClassNameValue; labelWrapper?: ClassNameValue; label?: ClassNameValue; container?: ClassNameValue; description?: ClassNameValue; error?: ClassNameValue; hint?: ClassNameValue; help?: ClassNameValue; }

插槽

插槽类型
label{ label?: string | undefined; }
提示{ hint?: string | undefined; }
description{ description?: string | undefined; }
帮助{ help?: string | undefined; }
error{ error?: string | boolean | undefined; }
default{ error?: string | boolean | undefined; }

主题

app.config.ts
export default defineAppConfig({
  ui: {
    formField: {
      slots: {
        root: '',
        wrapper: '',
        labelWrapper: 'flex content-center items-center justify-between',
        label: 'block font-medium text-default',
        container: 'mt-1 relative',
        description: 'text-muted',
        error: 'mt-2 text-error',
        hint: 'text-muted',
        help: 'mt-2 text-muted'
      },
      variants: {
        size: {
          xs: {
            root: 'text-xs'
          },
          sm: {
            root: 'text-xs'
          },
          md: {
            root: 'text-sm'
          },
          lg: {
            root: 'text-sm'
          },
          xl: {
            root: 'text-base'
          }
        },
        required: {
          true: {
            label: "after:content-['*'] after:ms-0.5 after:text-error"
          }
        }
      },
      defaultVariants: {
        size: 'md'
      }
    }
  }
})
vite.config.ts
import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import ui from '@nuxt/ui/vite'

export default defineConfig({
  plugins: [
    vue(),
    ui({
      ui: {
        formField: {
          slots: {
            root: '',
            wrapper: '',
            labelWrapper: 'flex content-center items-center justify-between',
            label: 'block font-medium text-default',
            container: 'mt-1 relative',
            description: 'text-muted',
            error: 'mt-2 text-error',
            hint: 'text-muted',
            help: 'mt-2 text-muted'
          },
          variants: {
            size: {
              xs: {
                root: 'text-xs'
              },
              sm: {
                root: 'text-xs'
              },
              md: {
                root: 'text-sm'
              },
              lg: {
                root: 'text-sm'
              },
              xl: {
                root: 'text-base'
              }
            },
            required: {
              true: {
                label: "after:content-['*'] after:ms-0.5 after:text-error"
              }
            }
          },
          defaultVariants: {
            size: 'md'
          }
        }
      }
    })
  ]
})

更新日志

dd81d— feat: 添加 data-slot 属性 (#5447)

5b177— feat: 扩展原生 HTML 属性 (#5348)

77a55— fix: 改进嵌套表单验证处理 (#5024)

5cb65— 特性:导入 @nuxt/ui-pro 组件

ec2bc— feat: 导出表单错误注入键 (#4808)

c64c4— fix: 解决轻微的可访问性和渲染问题 (#4515)

a4d0c— fix: 改进带有布尔值的 error 类型

127e0— chore: 更新所有非主要依赖项 (v3) (#4443)

459a0— fix: 对 errorhelp 插槽使用 div

e6e51— fix:class 应该优先于 ui 属性

39c86— fix:@nuxt/module-builder 升级后重构类型(#3855)

20c33— fix: 将 help 添加到 aria-describedby 属性 (#3691)

02184— chore: 改进 TSDoc (#3619)