Skip to content

Commit

Permalink
fix(form): 修复validator回调中第一个参数带有多余的引号(#2931) (#2933)
Browse files Browse the repository at this point in the history
* fix(form): 修复validator回调中第一个参数带有多余的引号(#2931)

* chore(form): 生成变更记录文件

* chore(form): 添加注释

---------

Co-authored-by: xiamiao <[email protected]>
  • Loading branch information
xiamiao1121 and xiamiao authored Jul 24, 2024
1 parent cafc0a5 commit 369cdf9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/curvy-falcons-confess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/form": patch
---

fix: 修复 validator 回调中第一个参数带有多余的引号
5 changes: 5 additions & 0 deletions .changeset/old-penguins-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@hi-ui/hiui": patch
---

fix(form): 修复 validator 回调中第一个参数带有多余的引号
20 changes: 18 additions & 2 deletions packages/ui/form/src/use-form-field.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,24 @@ export const useFormField = <Values = any>(props: UseFormFieldProps<Values>) =>

// 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]:
Expand Down

0 comments on commit 369cdf9

Please sign in to comment.