我正在编写一个 Grails(当前为 2.3.3)应用程序并创建了一个类似于以下内容的可验证命令对象:
@Validateable
class MyCustomCommand {
String name
static constraints = {
name blank: false
}
}
在我的 i18n/messages.properties
文件中,我定义了以下属性来覆盖默认错误消息。
MyCustomCommand.name.blank=Name must be provided.
MyCustomCommand.name.null=Name must be provided.
根据 Grails 文档,应该采用 [Class Name].[Property Name].[Constraint Code]
格式,正如我所做的那样。当我运行我的应用程序时,如果我将该值留空,我仍然会收到 null 属性的默认消息。
我也尝试按照默认消息的示例进行定义,但仍然得到默认消息。
MyCustomCommand.name.blank.message=Name must be provided.
MyCustomCommand.name.null.message=Name must be provided.
我假设我在这里遗漏了一些简单的东西,但还没有偶然发现什么。对我做错了什么有什么建议吗?
最佳答案
其实很简单。消息应如下所示:
myCustomCommand.name.blank=Name must be provided.
myCustomCommand.name.nullable=Name must be provided.
//className.propertyName.blank (camelCase with first letter of class name lower)
关于validation - Grails 为命令对象定义自定义错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21971543/