ant-design/ant-design

动态规则rules为空时,已校验出的错误无法被消除

Open

#51,008 建立於 2024年9月25日

在 GitHub 查看
 (6 留言) (0 反應) (0 負責人)TypeScript (98,031 star) (54,612 fork)batch import
Inactivehelp wanted

描述

Reproduction link

https://codesandbox.io/p/sandbox/antd-reproduction-template-forked-sr3vgc?workspaceId=f8c8b8be-9c36-4d85-adcf-c7766abab738

Steps to reproduce

  1. 选中昵称是否需要校验,此时会显示昵称的错误信息
  2. 取消昵称是否需要校验,此时不会取消昵称错误信息 image

What is expected?

当rules为空数组时,能正确的将错误信息去掉

What is actually happening?

当rules为空数组时,无法移除已经校验的错误信息

Environment Info
antd 4.24.16
React 17.x
System mac os
Browser chrome

经过断点排查发现这个错误来自rc-form-field/src/useForm.ts#904 当rule为空时,无法不再触发校验逻辑。 代码如下

app

目前的临时解决方案


/**
 * 空规则标识
 * gai
 */
const EMPTY_RULE_SYMBOL = Symbol('EMPTY_SYMBOL');

/**
 * 为了规避rc-form-field的校验bug
 * 具体代码可以参考rc-form-field/src/useForm.ts#904
 * @returns
 */
export const createEmptyRule = (): any => {
  return {
    [EMPTY_RULE_SYMBOL]: true,
  };
};

// 需要为空时
setRules([createEmptyRule]);

貢獻者指南