diff --git a/src/form/FormItem.tsx b/src/form/FormItem.tsx index 9151499b53..72eb10bfb1 100644 --- a/src/form/FormItem.tsx +++ b/src/form/FormItem.tsx @@ -45,6 +45,7 @@ export interface FormItemInstance { validate?: Function; resetField?: Function; setValidateMessage?: Function; + getValidateMessage?: Function; resetValidate?: Function; validateOnly?: Function; isFormList?: boolean; @@ -382,6 +383,10 @@ const FormItem = forwardRef((originalProps, ref setVerifyStatus(status); } + function getValidateMessage() { + return errorList; + } + useEffect(() => { // 注册自定义更新回调 if (!shouldUpdate || !form) return; @@ -459,6 +464,7 @@ const FormItem = forwardRef((originalProps, ref validateOnly, resetField, setValidateMessage, + getValidateMessage, resetValidate: resetHandler, }; useImperativeHandle(ref, (): FormItemInstance => instance); diff --git a/src/form/form.en-US.md b/src/form/form.en-US.md index 1b1362f802..a3d3a000d3 100644 --- a/src/form/form.en-US.md +++ b/src/form/form.en-US.md @@ -44,6 +44,7 @@ reset | `(params?: FormResetParams)` | \- | required。[see more ts de setFields | `(fields: FieldData[])` | \- | required。Typescript:`(fields: FieldData[]) => void` `interface FieldData { name: NamePath; value?: unknown, status?: string, validateMessage?: { type?: string, message?: string } }`。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/src/form/type.ts) setFieldsValue | `(field: Data)` | \- | required setValidateMessage | `(message: FormValidateMessage)` | \- | required。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/src/form/type.ts)。
`type FormValidateMessage = { [field in keyof FormData]: FormItemValidateMessage[] }`

`interface FormItemValidateMessage { type: 'warning' \| 'error'; message: string }`
+getValidateMessage | `(fields?: Array)` | `Array \| void` | required。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/src/form/type.ts)。
submit | `(params?: { showErrorMessage?: boolean })` | \- | required validate | `(params?: FormValidateParams)` | `Promise>` | required。[see more ts definition](https://github.com/Tencent/tdesign-react/blob/develop/src/form/type.ts)。
`interface FormValidateParams { fields?: Array; showErrorMessage?: boolean; trigger?: ValidateTriggerType }`

`type ValidateTriggerType = 'blur' \| 'change' \| 'submit' \| 'all'`
validateOnly | `(params?: Pick)` | `Promise>` | required diff --git a/src/form/form.md b/src/form/form.md index 5f5be5414a..90017236f1 100644 --- a/src/form/form.md +++ b/src/form/form.md @@ -126,6 +126,7 @@ reset | `(params?: FormResetParams)` | \- | 必需。重置表单, setFields | `(fields: FieldData[])` | \- | 必需。设置多组字段状态。TS 类型:`(fields: FieldData[]) => void` `interface FieldData { name: NamePath; value?: unknown, status?: string, validateMessage?: { type?: string, message?: string } }`。[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/form/type.ts) setFieldsValue | `(field: Data)` | \- | 必需。设置表单字段值 setValidateMessage | `(message: FormValidateMessage)` | \- | 必需。设置自定义校验结果,如远程校验信息直接呈现。注意需要在组件挂载结束后使用该方法。`FormData` 指表单数据泛型。[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/form/type.ts)。
`type FormValidateMessage = { [field in keyof FormData]: FormItemValidateMessage[] }`

`interface FormItemValidateMessage { type: 'warning' \| 'error'; message: string }`
+getValidateMessage | `(fields?: Array)` | `Array \| void` | 必需。获取校验结果,当调用 getValidateMessage() 时返回所有校验结果。[详细类型定义](https://github.com/Tencent/tdesign-react/blob/develop/src/form/type.ts)。
submit | `(params?: { showErrorMessage?: boolean })` | \- | 必需。提交表单,表单里面没有提交按钮`