Grails 自定义错误消息的问题

发布于 2024-09-09 07:55:21 字数 1206 浏览 7 评论 0原文

我目前正在尝试在 grails 中为默认约束指定自定义错误消息,但到目前为止我得到的只是默认错误消息。

我知道我必须编辑 grails-app/i18n/messages.properties 文件

如果我更改以下默认错误代码消息,它将正确显示新的错误消息

default.blank.message=Property [{0}] of class [{1}] cannot be blank

但是,这不是我想要做的。我需要更精细的错误报告,并且有多个可以为空的字段等。我希望能够做的是,显示类中每个字段的自定义消息

package com.mycompany.myapp

class Test{

 String name
 def constraints = {
 name(nullable:false, blank:false)
 }
}

(以下代码附加到 messages.properties 的末尾)

test.name.blank=Name cannot be blank
test.name.nullable=Name cannot be nullable

根据 grails 文档,无论有或没有包名称,这都应该正常工作 - className.propertyName.blank

grails.org/doc/latest/ (约束部分)& (第 7.4 节 - 验证和国际化)

我已经尝试了所有我能想到的组合,但它总是显示自定义消息

我也尝试安装 grails i18n 模板插件

http://www.grails.org/I18n+Templates+Plugin

它自动为我生成了错误代码。我将新的错误代码附加到现有 messages.properties 文件的末尾,但我仍然收到默认错误消息。

但是,该插件生成的错误代码有所不同。

而不是 grails 文档中指定的格式 - test.name.null=......,它自动生成 test.name.null.error=自定义消息

我也尝试完全删除默认错误消息,但它们是仍然显示

如果有人以前遇到过这个问题,我将不胜感激任何人可以给我的帮助

提前致谢

I am currently trying to specify custom error messages in grails for the default constraints but so far all I get back is the default error message.

I know that I have to edit the grails-app/i18n/messages.properties file

If I change the following default error codes message, it will correctly display the new error message

default.blank.message=Property [{0}] of class [{1}] cannot be blank

However, this is not what I am trying to do. I need more granular error reporting and have more than one field that can be blank etc. What I would like to be able to do would be, display custom messages for each field in a class

package com.mycompany.myapp

class Test{

 String name
 def constraints = {
 name(nullable:false, blank:false)
 }
}

(following codes appended to end of messages.properties)

test.name.blank=Name cannot be blank
test.name.nullable=Name cannot be nullable

According to the grails documentation this should work correctly, either with or without the package name - className.propertyName.blank

grails.org/doc/latest/ (constraints section) & (section 7.4 - validation & internationalization)

I have tried all comnbinations that I can think of, but it always displays the custom message

I have also tried installing the grails i18n templates plugin

http://www.grails.org/I18n+Templates+Plugin

which generated the error codes automatically for me. I appended the new error codes to the end of the existing messages.properties file but I still get the default error messages.

However, there was something different with the error codes that were generated by the plugin.

instead of the format specified in the grails doc - test.name.null=......, it automatically generated test.name.null.error=Custom Message

I have also tried deleting the default error messages completely, but they are still displayed

If anyone has experienced this issue before, I would appreciate any help that anyone can give me

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

无悔心 2024-09-16 07:55:21

put def messageSource (在控制器或服务中)

item.errors?.allErrors?.each{ 
println  messageSource.getMessage(it, null)
};

我还找到了一个很好的链接,可以更好地解释这一点

http://johnrellis.blogspot.com/2010/02/retrieve-grails-domain-errors-from.html

put def messageSource (in controller or service)

item.errors?.allErrors?.each{ 
println  messageSource.getMessage(it, null)
};

I also found a good link which explains this better

http://johnrellis.blogspot.com/2010/02/retrieve-grails-domain-errors-from.html

三寸金莲 2024-09-16 07:55:21

好吧,文档向您展示了如何覆盖默认验证约束之一(空白、可为空、最小值、最大值、大小、范围等)的消息的示例。但它无法告诉您查看每个约束的文档,并且在底部显示要使用的属性键:

错误代码:className.propertyName.size.toosmallclassName。 propertyName.size.toobig

用于约束 size http://grails.org/doc/latest/ref/Constraints/size.html

因此,

package com.example
class User {
    String username

    static constraints = {
        username  size:5..15
    }
}

使用:

com.example.User.username.size.toosmall=哟!太小:值为 [{2}] 的类 [{1}] 的 [{0}] 不在 [{3}] 到 [{4}] 的有效大小范围内

com.example.User .username.size.toobig=哟!太大:值为 [{2}] 的类 [{1}] 的 [{0}] 不在 [{3}] 到 [{4}] 的有效大小范围内

Well, the documentation shows you an example of how to override the messages for one of the default Validation Constraints (blank, nullable, min, max, size, range, etc.). But it fails to tell you to look in the documentation for each Constraint and at the bottom it shows you what propery key to use:

Error Code: className.propertyName.size.toosmall or className.propertyName.size.toobig

for Constraint size http://grails.org/doc/latest/ref/Constraints/size.html

So, for

package com.example
class User {
    String username

    static constraints = {
        username  size:5..15
    }
}

use:

com.example.User.username.size.toosmall=Yo there! too small: [{0}] of class [{1}] with value [{2}] does not fall within the valid size range from [{3}] to [{4}]

com.example.User.username.size.toobig=Yo there! too big: [{0}] of class [{1}] with value [{2}] does not fall within the valid size range from [{3}] to [{4}]

绝情姑娘 2024-09-16 07:55:21

您的约束可能不是静态的 - 应将其指定为“静态约束 = { ...”

另请注意,可空默认为 false,因此您无需指定它。

It might be that your constraints aren't static - it should be specified as "static constraints = { ..."

Also note that nullable defaults to false so you don't need to specify that.

℡寂寞咖啡 2024-09-16 07:55:21

我在 messages.properties 中使用完全限定的类名

com.shareyourlove.User.password.blank=Some custom message

I use fully qualified class names in my messages.properties

com.shareyourlove.User.password.blank=Some custom message
柠北森屋 2024-09-16 07:55:21

这对我有用

com.model.Customer.name.nullable.error = Custom message

而不是

com.model.Customer.name.blank = Custom message

This worked for me

com.model.Customer.name.nullable.error = Custom message

instead of

com.model.Customer.name.blank = Custom message
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文