Form 表单
WebH5
表单容器。托管数据 model 与校验规则 rules,配合 FormItem 完成字段校验、重置与整体禁用。
何时使用
需要收集并校验多个字段的表单场景:登录、注册、资料编辑、下单。
何时不使用
单个输入框就地提交(如搜索)不必套 Form。
Props
| 名称 | 类型 | 默认值 | 说明 |
|---|---|---|---|
model | object | - | 表单数据对象(必填),FormItem 的 prop 对应其 key |
rules | Record<string, FormRule[]> | - | 校验规则表,key 与 FormItem 的 prop 对应;FormRule: { required?, message, trigger?: 'change'|'blur', type?: 'email'|'url'|'number', min?, max?, pattern?, validator? };type 为内置快捷校验 |
labelPosition | string:'top' | 'left' | 'inline' | "top" | 标签位置;top 对移动端友好,left 适合桌面宽屏,inline 标签与控件同行(字段横向换行,适合搜索栏/筛选栏) |
labelWidth | string | "96px" | 标签宽度,labelPosition 为 left / inline 时生效 |
disabled | boolean | false | 整体禁用表单 |
scrollToError | boolean | "false" | 校验失败后自动平滑滚动到第一个错误字段 |
validateOnRuleChange | boolean | "true" | rules 变化时自动重新校验(深度监听) |
Slots
| 名称 | 说明 |
|---|---|
default | 一组 FormItem |
Methods
| 名称 | 签名 | 说明 |
|---|---|---|
validate | () => Promise<boolean> | 全量校验所有字段,全部通过返回 true |
validateField | (prop: string) => Promise<boolean> | 校验单个字段 |
resetFields | () => void | 恢复所有字段初始值并清除错误 |
clearValidate | () => void | 清除所有字段的错误提示 |
示例
登录表单(含校验规则)
示例加载中…
左侧标签(桌面宽屏)
示例加载中…
自定义校验规则
示例加载中…
异步校验:旧结果不覆盖新值
示例加载中…
重置与清除校验
示例加载中…
全局禁用
示例加载中…
业务场景:新建收货地址
示例加载中…
Playground
加载中…
top
跨端版本
该组件同时提供 uni-app / uni-app X 实现(H5 / 微信小程序 / App),API 可能略有差异,见 uni-app 端 Form 文档(含手机预览)。