From a9beed5895de88f78e7de2c3457e1328505ba3e0 Mon Sep 17 00:00:00 2001 From: longhao Date: Sat, 16 Dec 2023 21:42:48 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E8=A1=A5=E5=85=85=20FormItem=20?= =?UTF-8?q?=E6=96=87=E6=A1=A3=20getValueProps=20=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E9=A2=9D=E5=A4=96=E8=AF=B4=E6=98=8E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/form/index.en-US.md | 2 +- components/form/index.zh-CN.md | 22 +++++++++++++++++++++- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/components/form/index.en-US.md b/components/form/index.en-US.md index d54cfc65a4fc..d3f112f7b7e1 100644 --- a/components/form/index.en-US.md +++ b/components/form/index.en-US.md @@ -123,7 +123,7 @@ Form field component for data bidirectional binding, validation, layout, and so | dependencies | Set the dependency field. See [below](#dependencies) | [NamePath](#namepath)\[] | - | | | extra | The extra prompt message. It is similar to help. Usage example: to display error message and prompt message at the same time | ReactNode | - | | | getValueFromEvent | Specify how to get value from event or other onChange arguments | (..args: any\[]) => any | - | | -| getValueProps | Additional props with sub component | (value: any) => any | - | 4.2.0 | +| getValueProps | Additional props with sub component (the component will traverse the return value for update, and attribute values of type 'function' will be ignored, resulting in abnormal updates) | (value: any) => Record | - | 4.2.0 | | hasFeedback | Used with `validateStatus`, this option specifies the validation status icon. Recommended to be used only with `Input`. Also, It can get feedback icons via icons prop. | boolean \| { icons: [FeedbackIcons](#feedbackicons) } | false | icons: 5.9.0 | | help | The prompt message. If not provided, the prompt message will be generated by the validation rule. | ReactNode | - | | | hidden | Whether to hide Form.Item (still collect and validate value) | boolean | false | 4.4.0 | diff --git a/components/form/index.zh-CN.md b/components/form/index.zh-CN.md index 610371470735..13524b03c926 100644 --- a/components/form/index.zh-CN.md +++ b/components/form/index.zh-CN.md @@ -124,7 +124,7 @@ const validateMessages = { | dependencies | 设置依赖字段,说明[见下](#dependencies) | [NamePath](#namepath)\[] | - | | | extra | 额外的提示信息,和 `help` 类似,当需要错误信息和提示文案同时出现时,可以使用这个。 | ReactNode | - | | | getValueFromEvent | 设置如何将 event 的值转换成字段值 | (..args: any\[]) => any | - | | -| getValueProps | 为子元素添加额外的属性 | (value: any) => any | - | 4.2.0 | +| getValueProps | 为子元素添加额外的属性 (组件会依赖返回值对比更新,类型为 'function' 的属性值会被忽略,导致异常更新) | (value: any) => Record | - | 4.2.0 | | hasFeedback | 配合 `validateStatus` 属性使用,展示校验状态图标,建议只配合 Input 组件使用 此外,它还可以通过 Icons 属性获取反馈图标。 | boolean \| { icons: [FeedbackIcons](#feedbackicons) } | false | icons: 5.9.0 | | help | 提示信息,如不设置,则会根据校验规则自动生成 | ReactNode | - | | | hidden | 是否隐藏字段(依然会收集和校验字段) | boolean | false | 4.4.0 | @@ -566,6 +566,26 @@ Form.Item 默认绑定值属性到 `value` 上,而 Switch、Checkbox 等组件 ``` +### 自定义 validator 没有效果 + +这是由于你的 `validator` 有错误导致 `callback` 没有执行到。你可以选择通过 `async` 返回一个 promise 或者使用 `try...catch` 进行错误捕获: + +```jsx +validator: async (rule, value) => { + throw new Error('Something wrong!'); +} + +// or + +validator(rule, value, callback) => { + try { + throw new Error('Something wrong!'); + } catch (err) { + callback(err); + } +} +``` + ### name 为数组时的转换规则? 当 `name` 为数组时,会按照顺序填充路径。当存在数字且 form store 中没有该字段时会自动转变成数组。因而如果需要数组为 key 时请使用 string 如:`['1', 'name']`。