Grails 验证错误 - 开发人员与用户
将在 Grails 域对象上调用 validate() 时收到的面向开发人员的验证错误列表转换为面向用户的错误消息的最佳方法是什么?
示例:
值为 [x] 的类 [class testproj.AuthUser] 的属性 [email] 不是有效的电子邮件地址,
宁愿读取它:
给出的电子邮件不是有效的电子邮件地址
是否有任何内置功能已经可以做到这一点?
What is the best way to convert the developer-oriented validation error list you receive from calling validate() on a Grails domain object into user-oriented error messages?
Example:
Property [email] of class [class testproj.AuthUser] with value [x] is not a valid e-mail address
would rather have it read:
Email given is not a valid e-mail address
Is there anything built-in that already does this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
grails-app/i18n
中的messages.properties
文件查看此文档
http://grails.org/doc/latest/guide/7.%20Validation .html
要获取所有错误代码,请查看此处 http://grails.org/doc/latest/ref/Constraints/email.html。启用右侧的快速导航。每个约束的底部是用于更改错误消息的属性键。
the
messages.properties
files ingrails-app/i18n
check out this documentation
http://grails.org/doc/latest/guide/7.%20Validation.html
To get all the error codes, look here http://grails.org/doc/latest/ref/Constraints/email.html. Enable the quick nav on the right. At the bottom of every constraint is the properties key that you use to change the error message.
还有一个不错的插件,名为:
grails install-plugin i18n-templates
,可以在此处找到http://www.grails.org/plugin/i18n-templates
例如,您在“org.example.User”中有一个 Domain 类,并且您想要指定本地化消息。使用以下命令:
grailsgenerate-i18n-messages org.example.User
这将在控制台中为您生成可能的消息列表。
将此输出复制并粘贴到英语的
messages.properties
中,以及您想要本地化的语言的所有其他 message.properties 中。然后您只需编辑这些属性文件即可本地化您的页面。
Eclipse/STS 有一个很好的 GUI,这使得这些属性文件的本地化速度更快。
问候,
简
There is also a nice plugin called:
grails install-plugin i18n-templates
which can be found here http://www.grails.org/plugin/i18n-templates
e.g. you have a Domain class in 'org.example.User' and you want to specify the localization messages. Use the following command:
grails generate-i18n-messages org.example.User
This will generate a list of possible messages for you in the console.
Copy and paste this output into your
messages.properties
for the English language and in every other message.properties for the language you want to have localizations.Then you just have to edit these property-files in order to have your page localized.
Eclipse/STS has a nice GUI, which makes localization of these property-files even faster.
Greetings,
Jan