Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

form error feedback need support label instead of namepath #3255

Closed
cwjTerrace opened this issue Jul 8, 2022 · 6 comments
Closed

form error feedback need support label instead of namepath #3255

cwjTerrace opened this issue Jul 8, 2022 · 6 comments
Labels

Comments

@cwjTerrace
Copy link

This function solves the problem (这个功能解决的问题)

当我使用多语言的时候,配置了:validate-messages="{ required: '请输入 %s' }",报错信息用户看到的报错信息是“请输入 user.name” ,使用 namepath 而不是label 会很疑惑。

Expected API (期望的 API)

希望支持报错信息用户看到的报错信息是“请输入 姓名”

https://codesandbox.io/s/hidden-monad-106ogf?file=/src/Demo.vue:338-381

@github-actions github-actions bot added the feature request New feature or request label Jul 8, 2022
@07akioni
Copy link
Collaborator

07akioni commented Jul 10, 2022

Label 目前没法返回实际上是 async validator 的实现导致的,没有办法再进行封装从 ValidateError 中拿到 label,如果 async-validator 未来能增加一些相关的 feature,我倒是觉得把这个功能补上挺好的

@07akioni
Copy link
Collaborator

image

ref yiminghe/async-validator#313

@cwjTerrace
Copy link
Author

这个我看过了,antd用的也是async validator,但是他的报错提示就是Label,他把报错结果处理了一下

  const fillVariableResult = result.map(error => {
    if (typeof error === 'string') {
      return replaceMessage(error, kv);
    }
    return error;
  });
/**
 * Replace with template.
 *   `I'm ${name}` + { name: 'bamboo' } = I'm bamboo
 */
function replaceMessage(template: string, kv: Record<string, string>): string {
  return template.replace(/\$\{\w+\}/g, (str: string) => {
    const key = str.slice(2, -1);
    return kv[key];
  });
}

@07akioni
Copy link
Collaborator

这个我看过了,

没想到啊,居然自己人 hack 自己人

@07akioni
Copy link
Collaborator

这个我看过了,antd用的也是async validator,但是他的报错提示就是Label,他把报错结果处理了一下

  const fillVariableResult = result.map(error => {
    if (typeof error === 'string') {
      return replaceMessage(error, kv);
    }
    return error;
  });
/**
 * Replace with template.
 *   `I'm ${name}` + { name: 'bamboo' } = I'm bamboo
 */
function replaceMessage(template: string, kv: Record<string, string>): string {
  return template.replace(/\$\{\w+\}/g, (str: string) => {
    const key = str.slice(2, -1);
    return kv[key];
  });
}

看起来只有 string 的情况下才 work,我当时应该是简单看过源码也考虑过类似的问题,就是处理不干净

@jahnli
Copy link
Collaborator

jahnli commented Mar 12, 2024

This issue does not have any recent activity. If you are still experiencing similar problems, open a new error, including a minimal copy of the problem

@jahnli jahnli closed this as completed Mar 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants