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

建议logic层,自定义校验增加参数,来实现如 “中文提示” 这样的功能 #421

Closed
niejimao opened this issue Aug 11, 2016 · 5 comments

Comments

@niejimao
Copy link

DESC

建议logic层,自定义校验增加参数,来实现如 “中文提示” 这样的功能

ENV

Platform:

Node.js Version:

ThinkJS Version:

@welefen
Copy link
Member

welefen commented Aug 11, 2016

中文提示可以在语言包里配置,https://thinkjs.org/zh-cn/doc/2.2/logic.html#toc-460

@niejimao
Copy link
Author

可能我描述不清,详情请看thinkjs社区的问题 http://wenda.thinkjs.org/?/question/51

@jinmarcus
Copy link

jinmarcus commented Dec 28, 2016

目前我的解决办法是替换,比如返回的错误字段是这样的{name: "name不能为空"}, 那么就拿到key ,就是name,手动替换掉"name 不能为空"中的name为"姓名",当然这种办法比较笨,只能说不破坏源码的情况下解决我的问题。当然如果官方可以提供更好的自定义错误提示的话,那就最好了,因为在不同场景下也许报错的信息是不一样的,如果统一在语言模板里设置的话未免太粗暴了一点,以上是我的一点微薄的建议。

@lushijie
Copy link
Member

lushijie commented Jul 19, 2017

thinkjs3.0 中,校验框架支持了aliasName 配置,可以指定别名:

module.exports = class extends think.Logic {
  indexAction(){
    this.rules = {
      username: {
        required: true
      }
    }
  }
}

对于上述规则,在验证失败的情况下 this.validateErrors 将为 {username: 'username can not be blank'}。但是有时想让错误自定义为 '用户名 can not be blank'。需要如下操作:

module.exports = class extends think.Logic {
  indexAction(){
    this.rules = {
      username: {
        required: true,
        aliasName: '用户名'
      }
    }
  }
}

可以在 src/config/validator.js 中复写掉默认的 required 错误信息:

module.exports = {
  messages: {
    required: '{name} 不能为空',
  }
}

这样就可以得到 {name: 用户名不能为空}

@welefen
Copy link
Member

welefen commented Aug 11, 2017

3.0 里有 aliasName 来实现

@welefen welefen closed this as completed Aug 11, 2017
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

No branches or pull requests

4 participants