--- title: Form 表单 description: 用以收集、校验和提交数据,一般由输入框、单选框、复选框、选择器等控件组成。 spline: base isComponent: true toc: false --- ## 引入 可在 `main.ts` 或在需要使用的页面或组件中引入。 ```js import TForm from '@tdesign/uniapp/form/form.vue'; import TFormItem from '@tdesign/uniapp/form-item/form-item.vue'; ``` ### 01 组件类型 基础表单 {{ horizontal }} {{ vertical }} ## API ### Form Props 名称 | 类型 | 默认值 | 描述 | 必传 -- | -- | -- | -- | -- custom-style | Object | - | 自定义样式 | N colon | Boolean | false | 是否在表单标签字段右侧显示冒号 | N content-align | String | left | 表单内容对齐方式:左对齐、右对齐。可选项:left/right | N data | Object | {} | 表单数据。TS 类型:`FormData` | N disabled | Boolean | undefined | 是否禁用整个表单 | N error-message | Object | - | 表单错误信息配置,示例:`{ idcard: '请输入正确的身份证号码', max: '字符长度不能超过 ${max}' }`。TS 类型:`FormErrorMessage` | N label-align | String | right | 表单字段标签对齐方式:左对齐、右对齐、顶部对齐。可选项:left/right/top | N label-width | String / Number | '81px' | 可以整体设置label标签宽度,默认为81px | N readonly | Boolean | undefined | 是否整个表单只读 | N required-mark | Boolean | undefined | 是否显示必填符号(*),默认显示 | N required-mark-position | String | - | 表单必填符号(*)显示位置。可选项:left/right | N reset-type | String | empty | 重置表单的方式,值为 empty 表示重置表单为空,值为 initial 表示重置表单数据为初始值。可选项:empty/initial | N rules | Object | - | 表单字段校验规则。TS 类型:`FormRules` `type FormRules = { [field in keyof T]?: Array }`。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/form/type.ts) | N scroll-to-first-error | String | - | 表单校验不通过时,是否自动滚动到第一个校验不通过的字段,平滑滚动或是瞬间直达。值为空则表示不滚动。可选项:''/smooth/auto | N show-error-message | Boolean | true | 校验不通过时,是否显示错误提示信息,统一控制全部表单项。如果希望控制单个表单项,请给 FormItem 设置该属性 | N submit-with-warning-message | Boolean | false | 【讨论中】当校验结果只有告警信息时,是否触发 `submit` 提交事件 | N ### Form Events 名称 | 参数 | 描述 -- | -- | -- reset | `(context: { e?: FormResetEvent })` | 表单重置时触发。[通用类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/common/common.ts) submit | `(context: SubmitContext)` | 表单提交时触发。其中 `context.validateResult` 表示校验结果,`context.firstError` 表示校验不通过的第一个规则提醒。`context.validateResult` 值为 `true` 表示校验通过;如果校验不通过,`context.validateResult` 值为校验结果列表。
【注意】⚠️ 默认情况,输入框按下 Enter 键会自动触发提交事件,如果希望禁用这个默认行为,可以给输入框添加 enter 事件,并在事件中设置 `e.preventDefault()`。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/form/type.ts)。
`interface SubmitContext { e?: FormSubmitEvent; validateResult: FormValidateResult; firstError?: string; fields?: any }`

`type FormValidateResult = boolean \| ValidateResultObj`

`type ValidateResultObj = { [key in keyof T]: boolean \| ValidateResultList }`

`type ValidateResultList = Array`

`type AllValidateResult = CustomValidateObj \| ValidateResultType`

`interface ValidateResultType extends FormRule { result: boolean }`

`type ValidateResult = { [key in keyof T]: boolean \| ErrorList }`

`type ErrorList = Array`
validate | `(result: ValidateResultContext)` | 校验结束后触发,result 值为 true 表示校验通过;如果校验不通过,result 值为校验结果列表。[详细类型定义](https://github.com/Tencent/tdesign-miniprogram/tree/develop/packages/uniapp-components/form/type.ts)。
`type ValidateResultContext = Omit, 'e'>`
### FormInstanceFunctions 组件实例方法 名称 | 参数 | 返回值 | 描述 -- | -- | -- | -- clear-validate | `(fields?: Array)` | \- | 必需。清空校验结果。可使用 fields 指定清除部分字段的校验结果,fields 值为空则表示清除所有字段校验结果。清除邮箱校验结果示例:`clearValidate(['email'])` reset | `(params?: FormResetParams)` | \- | 必需。重置表单,表单里面没有重置按钮`