From 369cdf935fb4e3a81478dcbd8e199c1a7cac1875 Mon Sep 17 00:00:00 2001 From: xiamiao1121 <74885998+xiamiao1121@users.noreply.github.com> Date: Wed, 24 Jul 2024 17:26:21 +0800 Subject: [PATCH] =?UTF-8?q?fix(form):=20=E4=BF=AE=E5=A4=8Dvalidator?= =?UTF-8?q?=E5=9B=9E=E8=B0=83=E4=B8=AD=E7=AC=AC=E4=B8=80=E4=B8=AA=E5=8F=82?= =?UTF-8?q?=E6=95=B0=E5=B8=A6=E6=9C=89=E5=A4=9A=E4=BD=99=E7=9A=84=E5=BC=95?= =?UTF-8?q?=E5=8F=B7(#2931)=20(#2933)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * fix(form): 修复validator回调中第一个参数带有多余的引号(#2931) * chore(form): 生成变更记录文件 * chore(form): 添加注释 --------- Co-authored-by: xiamiao --- .changeset/curvy-falcons-confess.md | 5 +++++ .changeset/old-penguins-grin.md | 5 +++++ packages/ui/form/src/use-form-field.ts | 20 ++++++++++++++++++-- 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 .changeset/curvy-falcons-confess.md create mode 100644 .changeset/old-penguins-grin.md diff --git a/.changeset/curvy-falcons-confess.md b/.changeset/curvy-falcons-confess.md new file mode 100644 index 000000000..cb78995bc --- /dev/null +++ b/.changeset/curvy-falcons-confess.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/form": patch +--- + +fix: 修复 validator 回调中第一个参数带有多余的引号 diff --git a/.changeset/old-penguins-grin.md b/.changeset/old-penguins-grin.md new file mode 100644 index 000000000..f423155fa --- /dev/null +++ b/.changeset/old-penguins-grin.md @@ -0,0 +1,5 @@ +--- +"@hi-ui/hiui": patch +--- + +fix(form): 修复 validator 回调中第一个参数带有多余的引号 diff --git a/packages/ui/form/src/use-form-field.ts b/packages/ui/form/src/use-form-field.ts index d5a64da5a..d07e3194d 100644 --- a/packages/ui/form/src/use-form-field.ts +++ b/packages/ui/form/src/use-form-field.ts @@ -38,8 +38,24 @@ export const useFormField = (props: UseFormFieldProps) => // TODO: rules 处理成 Async Validate 的指定结构 const fieldMD5 = stringify(field as FormFieldPath) - - const validater = new Validater({ [fieldMD5]: fieldRules }) + const modifiedFieldRules = fieldRules.map((rule) => { + // 重写 rule 的 validator 函数,正则匹配 validatorRule 中的 field 和 fullField,消除 field 和 fullField 中的双引号 + // issue:https://github.com/XiaoMi/hiui/issues/2931 + if (rule.validator) { + return { + ...rule, + validator: (validatorRule: any, value: any, cb: any) => { + const field = validatorRule.field.replace(/"/g, '') + const fullField = validatorRule.fullField.replace(/"/g, '') + rule.validator({ ...validatorRule, field, fullField }, value, cb) + }, + } + } else + return { + ...rule, + } + }) + const validater = new Validater({ [fieldMD5]: modifiedFieldRules }) return validater.validate( { [fieldMD5]: