Skip to content

fix(Form): reset lastIndex before testing pattern rule - #6982

Merged
uyarn merged 2 commits into
Tencent:developfrom
kwy404:fix/form-pattern-global-regexp
Sep 29, 2026
Merged

uyarn merged 2 commits into
Tencent:developfrom
kwy404:fix/form-pattern-global-regexp

Conversation

@kwy404

@kwy404 kwy404 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

这个 PR 的性质是?

  • 日常 bug 修复
  • 新特性提交
  • 文档改进
  • 演示代码改进
  • 组件样式/交互改进
  • CI/CD 改进
  • 重构
  • 代码风格优化
  • 测试用例
  • 分支合并
  • 其他

相关 Issue

No existing issue.

需求背景和解决方案

Root cause: the pattern validator calls test() on the RegExp object from the rule. A regexp with the g or y flag is stateful: test() starts at lastIndex and moves it past the match. The same rule object is reused for every validation, so a valid value passes once and fails the next time. Example: with rules: { name: [{ pattern: /^\d+$/g }] } and name = '123', calling form.validate() three times returns true, an error, then true.

Fix: set reg.lastIndex = 0 before test(), so every validation starts at the beginning of the value (async-validator does the same for its pattern rule). Regexps without g or y ignore lastIndex, so they behave exactly as before.

Test: added pattern with global flag in form.utils.test.tsx, which validates '123' twice against /^\d+$/g. Before the fix the second call returns false (1 failed, 20 passed). After the fix all 21 tests pass, and the whole Form suite passes (91 passed, 3 todo). Prettier, ESLint and tsc --noEmit pass.

📝 更新日志

tdesign-vue-next

  • fix(Form): 修复 pattern 规则使用带 g 或 y 标志的正则时,连续校验隔次失败的问题

@tdesign-vue-next/chat

@tdesign-vue-next/nuxt

@tdesign-vue-next/auto-import-resolver

请求合并前的自查清单

  • 文档已补充或无须补充
  • 代码演示已提供或无须提供
  • TypeScript 定义已补充或无须补充
  • Changelog 已提供或无须提供

@tdesign-bot

Copy link
Copy Markdown
Collaborator

TDesign Component Site Preview Open

Component Preview
tdesign-vue-next 完成
@tdesign-vue-next/chat 完成

@pkg-pr-new

pkg-pr-new Bot commented Sep 28, 2026

Copy link
Copy Markdown
  • tdesign-vue-next-demo

    npm i https://pkg.pr.new/@tdesign-vue-next/auto-import-resolver@6982
    
    npm i https://pkg.pr.new/tdesign-vue-next@6982
    
    npm i https://pkg.pr.new/@tdesign-vue-next/chat@6982
    
    npm i https://pkg.pr.new/@tdesign-vue-next/nuxt@6982
    

commit: 85d2694

@uyarn

uyarn commented Sep 29, 2026

Copy link
Copy Markdown
Collaborator

Thanks

@uyarn
uyarn merged commit e41a16e into Tencent:develop Sep 29, 2026
1 check passed
@tdesign-bot tdesign-bot mentioned this pull request Sep 30, 2026
6 of 16 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants