Skip to content

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

@LimMem

Description

@LimMem

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]);

Metadata

Metadata

Assignees

No one assigned

    Labels

    Inactivehelp wantedThe suggestion or request has been accepted, we need you to help us by sending a pull request.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions