Skip to content

fix(TableForm): validateWholeFields() repeated calls and error overwrite#1234

Open
MrWindlike wants to merge 2 commits intov490-devfrom
fix/tableform-validateWholeFields-repeated-call
Open

fix(TableForm): validateWholeFields() repeated calls and error overwrite#1234
MrWindlike wants to merge 2 commits intov490-devfrom
fix/tableform-validateWholeFields-repeated-call

Conversation

@MrWindlike
Copy link
Contributor

@MrWindlike MrWindlike commented Feb 6, 2026

Summary

  • Bug 1 — 重复调用校验失效: validateAll 是 boolean 状态,setValidateAll(true) 在已为 true 时是 React no-op,不触发 re-render,formValidMapRef 被清空后无法重新填充,isValid() 因 vacuous truth 返回 true
  • Bug 2 — 校验结果被覆盖: triggerValidate() 设置的 validateResult 会被 error prop sync effect 覆盖清空,因为 setIsTouched(true) 导致 isTouched 变化触发该 effect,而 error prop 为 undefined 时直接将 validateResult 置为 undefined

Fix

  1. validateAll: boolean 改为 validateAllCount: number(递增计数器),确保每次 validateWholeFields() 都触发 re-render
  2. prevErrorRef 追踪 error prop 前值,仅在 error 确实从有值变为空时才清除 validateResult,避免 isTouched 变化时误清校验结果

Changed files

File Change
types.ts validateAll: booleanvalidateAllCount: number
index.tsx useState(false)useState(0), setValidateAll(true)setValidateAllCount(c => c + 1)
TableFormBodyCell.tsx 使用 validateAllCount;添加 prevErrorRef 防止 error sync effect 覆盖 validator 结果
TableFormBodyRows.tsx 同步更新 prop 名

Test plan

  • 调用 validateWholeFields() 一次,无效数据 → isValid() 返回 false
  • 不修改数据,再次调用 validateWholeFields()isValid() 仍为 false
  • 修正数据后调用 validateWholeFields()isValid() 返回 true
  • 验证重复调用后错误 UI 正确展示(未被清空)
  • 验证 errors prop 外部设置/清除仍然正常工作

🤖 Generated with Claude Code

…alidation

Replace boolean validateAll state with a counter (validateAllCount) so that
each call to validateWholeFields() increments the value and always triggers
a React re-render. Previously, calling setValidateAll(true) when already true
was a no-op, causing the validation useEffect to skip and isValid() to
incorrectly return true due to vacuous truth on an empty formValidMapRef.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Feb 6, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
cloudtower-ui-kit Error Error Feb 6, 2026 7:25am

…r results

When validateWholeFields() triggers setIsTouched(true), the isTouched change
causes the error prop sync useEffect to fire. Since the error prop is typically
undefined, it was overwriting the validateResult just set by triggerValidate().

Use a prevErrorRef to track whether the error prop actually changed, and only
clear validateResult when the error prop transitions from truthy to falsy,
not when isTouched changes with a falsy error.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@MrWindlike MrWindlike changed the title fix(TableForm): repeated validateWholeFields() calls bypass validation fix(TableForm): validateWholeFields() repeated calls and error overwrite Feb 6, 2026
@MrWindlike MrWindlike added the fix label Feb 6, 2026
@chenenpei
Copy link
Contributor

能不能补充下 bug 在业务上的现象?

msg: error,
isError: true,
});
} else if (errorChanged) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

我没看懂这个改动的逻辑。

如果 isTouched 为 true,又有 erro 就设置 error。如果 error 是 undefined 还要比较一次,这是为什么呢?为什么不多做这一次比较会有问题呢?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants