修改控制器中嵌入的命令对象的错误消息

时间:2012-02-08 17:27:33

标签: grails

我正在尝试修改messages.properties文件,以获取由控制器中指定的命令对象验证的表单输入。我从标准错误消息中获得的输出(我稍微修改以确保我遇到特定的错误消息)是:

电子邮件不能为空测试类com.dashboard.RegisterController $ DashboardUserRegistrationCommand

但没有com.dashboard.RegisterController的变体$ DashboardUserRegistrationCommand.null.message 工作

我想知道正确的规范应该是什么。

3 个答案:

答案 0 :(得分:0)

尝试将DashboardUserRegistrationCommand放在RegisterController的外部(下方)但仍在同一个文件中。然后 com.dashboard.DashboardUserRegistrationCommand .. 应该可以。

即。 com.dashboard.DashboardUserRegistrationCommand.message.nullable

答案 1 :(得分:0)

错误消息的典型布局是:

${packageName}.${className}.${propertyName}.${errorCode}

因此,对于您的示例,它将是:

com.dashboard.DashboardUserRegistrationCommand.message.nullable

在右侧的Grails Reference中有一个标题为'Constraints'的标题。在该标题下列出的特定约束的每个页面上,${errorCode}值列在页面底部。

有时您必须重新启动run-app以获取要在Grails项目中填充的新消息。

答案 2 :(得分:0)

为了将来帮助别人,我遇到了同样的问题,我的问题就是我定义我的密钥的方式,我现在使用:

对于默认消息: default.null.message =为{0}

写一个值

对于commmand错误消息: my.package.UserCommand.name.nullable =请告诉我们你的名字

奇怪的是,有时你使用nullable,有时你使用null。最好的事情是直接进入Grails Constraints并检查它是如何完成的,例如:

http://grails.org/doc/latest/ref/Constraints/nullable.html

相关问题