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]: