-
-
Notifications
You must be signed in to change notification settings - Fork 617
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
Comments
中文提示可以在语言包里配置,https://thinkjs.org/zh-cn/doc/2.2/logic.html#toc-460 |
可能我描述不清,详情请看thinkjs社区的问题 http://wenda.thinkjs.org/?/question/51 |
目前我的解决办法是替换,比如返回的错误字段是这样的{name: "name不能为空"}, 那么就拿到key ,就是name,手动替换掉"name 不能为空"中的name为"姓名",当然这种办法比较笨,只能说不破坏源码的情况下解决我的问题。当然如果官方可以提供更好的自定义错误提示的话,那就最好了,因为在不同场景下也许报错的信息是不一样的,如果统一在语言模板里设置的话未免太粗暴了一点,以上是我的一点微薄的建议。 |
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: '用户名'
}
}
}
} 可以在 module.exports = {
messages: {
required: '{name} 不能为空',
}
} 这样就可以得到 {name: 用户名不能为空} |
3.0 里有 aliasName 来实现 |
DESC
建议logic层,自定义校验增加参数,来实现如 “中文提示” 这样的功能
ENV
Platform:
Node.js Version:
ThinkJS Version:
The text was updated successfully, but these errors were encountered: