Grails自定义错误消息的问题 [英] Grails Problem with custom error messages

查看:142
本文介绍了Grails自定义错误消息的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



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



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

  default.blank.message =类[{1}]的属性[{0}]不能为空

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

  package com.mycompany.myapp 

class Test {

字符串名称
def constraints = {
名称(可空:假,空白:假)
}
}

(在messages.properties结尾附加代码)

  test.name.blank =名称不能为空
test.name.nullable =名称不能为空

根据grails文档,这应该可以正常工作,有或没有包名 - className.propertyName.blank



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



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



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

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



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



但是,错误代码有些不同这是由插件生成的。



代替了grails doc中指定的格式 - test.name.null = ......,它会自动生成测试。 name.null.error = Custom Message



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



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



预先感谢

解决方案

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

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

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

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


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

解决方案

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

这篇关于Grails自定义错误消息的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆