Files

219 lines
6.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: FieldError 字段错误
description: 展示表单字段校验错误消息
component: field-error
group: 组件
---
## 用法
```vue
<script setup lang="ts">
import { FieldError, TextField, Label, Input } from "@heroui-vue/components";
</script>
```
<ComponentPreview name="field-error-basic" />
FieldError 组件展示表单字段的校验错误消息。当父字段标记为无效时自动显示,并提供平滑的透明度过渡。
## 示例
### 基础校验
```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const value = ref("");
const isInvalid = computed(
() => value.value.length > 0 && value.value.length < 3,
);
</script>
<template>
<TextField v-model="value" class="w-64" :is-invalid="isInvalid">
<Label for="username">用户名</Label>
<Input id="username" placeholder="输入用户名" />
<FieldError>用户名至少需要 3 个字符</FieldError>
</TextField>
</template>
```
### 动态错误信息
使用 **FieldError** 作用域插槽读取当前字段错误。`is-invalid` 只负责展示;阻止提交请使用 `validate` 或 Form 的 `validation-errors`。下面示例用 `validate` + `validation-behavior="aria"`,输入时即可看到动态消息:
```vue
<script setup lang="ts">
import { ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const password = ref("");
function validatePassword(value: string) {
if (value.length === 0) return true;
return value.length < 8 ? "密码至少需要 8 个字符" : true;
}
</script>
<template>
<TextField
v-model="password"
name="password"
validation-behavior="aria"
:validate="validatePassword"
>
<Label>密码</Label>
<Input type="password" placeholder="至少 8 个字符" />
<FieldError>
<template #default="{ validationErrors }">
{{ validationErrors.join(",") }}
</template>
</FieldError>
</TextField>
</template>
```
上游 `FieldError` 的 render 函数 `(validation) => …` 在 Vue 中为 **FieldError** 的作用域插槽 `#default="{ validationErrors, isInvalid }"`。当前未提供完整 React Aria `validationDetails` 对象。
### 自定义校验逻辑
```vue
<script setup lang="ts">
import { computed, ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const email = ref("");
const isInvalid = computed(
() => email.value.length > 0 && !email.value.includes("@"),
);
</script>
<template>
<TextField v-model="email" :is-invalid="isInvalid">
<Label>邮箱</Label>
<Input type="email" />
<FieldError>邮箱必须包含 @ 符号</FieldError>
</TextField>
</template>
```
也可以用 `validate` 做同步提交拦截:
```vue
<TextField
v-model="email"
:validate="(v) => (v.includes('@') ? true : '邮箱必须包含 @ 符号')"
name="email"
>
<Label>邮箱</Label>
<Input type="email" />
<FieldError />
</TextField>
```
### 多条错误信息
默认插槽展示合并后的错误文本;需要逐条渲染时使用 FieldError 作用域插槽。下面示例的 `validate` 会返回多条错误:
```vue
<script setup lang="ts">
import { ref } from "vue";
import { FieldError, Input, Label, TextField } from "@heroui-vue/components";
const password = ref("");
function validatePassword(value: string) {
if (value.length === 0) return true;
const errors: string[] = [];
if (value.length < 8) errors.push("密码至少需要 8 个字符");
if (!/[A-Z]/.test(value)) errors.push("需要包含一个大写字母");
return errors.length ? errors : true;
}
</script>
<template>
<TextField
v-model="password"
name="password"
validation-behavior="aria"
:validate="validatePassword"
>
<Label>密码</Label>
<Input type="password" placeholder="试试 abc" />
<FieldError>
<template #default="{ validationErrors }">
<div v-for="(error, i) in validationErrors" :key="i">{{ error }}</div>
</template>
</FieldError>
</TextField>
</template>
```
## 自定义样式
### Tailwind CSS
<ComponentPreview name="field-error-custom-styles" />
### 全局 CSS
可使用 `@layer components` 指令自定义 FieldError 组件类。
```css
@layer components {
.hv-field-error {
@apply font-medium text-danger;
}
}
```
## 样式参考
HeroUI 遵循 [BEM](https://getbem.com/) 方法论,确保组件变体与状态可复用且易于自定义。类名前缀为 `hv-`。
### CSS 类
FieldError 组件使用以下 CSS 类([查看源码样式](https://github.com/heroui-inc/heroui/blob/v3/packages/styles/components/field-error.css)):
> 变体映射 `fieldErrorVariants` 从 `@heroui-vue/styles` 公开(`@heroui-vue/components` 再导出同一函数与 `FieldErrorVariants` 类型);组件渲染的类名由该映射生成。
#### 基础类 [!toc]
- `.hv-field-error` - 基础错误样式,danger 色
- 仅在存在 `data-visible` 属性时显示
- 长消息自动换行(`wrap-break-word`)
## API 参考
### FieldError
| Prop | 类型 | 默认值 | 描述 |
| -------- | --------------------------- | ------ | ---------------------------------------------------- |
| `id` | `string` | 自动 | 显式 id;未指定时生成并关联到控件 `aria-describedby` |
| 默认插槽 | `Component` | - | 错误消息;作用域提供 `validationErrors`、`isInvalid` |
| `class` | `string \| Array \| Object` | - | 附加 CSS 类(Vue class 绑定) |
字段无效时渲染;默认展示错误数组(空格连接)。在 Switch 等 toggle 字段内也可使用。
## 无障碍
FieldError 组件通过以下方式确保无障碍性:
- 使用 `role="alert"` 播报错误
- 语义化 HTML 支持屏幕阅读器
- 提供视觉与程序化错误指示(`aria-describedby` 关联)
- 根据校验状态自动管理可见性
当前不提供完整 React Aria `validationDetails` 对象;第三方契约请勿据此宣称完全兼容。
## 相关组件
[TextField 文本输入框](./text-field) · [Input 输入框](./input) · [TextArea 多行文本框](./textarea)
### validationDetails
作用域插槽还提供 `validationDetails?: ValidityState`。TextField/SearchField 使用真实控件的原生快照,OTP 可显式传入详情(优先)或读取其提交代理快照;其他尚未接入详情的字段为 undefined。可读取 `valueMissing`、`patternMismatch`、`typeMismatch`、`customError` 等原生标志,而不复制字段状态。详情本身不替代 `isInvalid` 或错误文本。