2011-01-12 65 views
0

我尝试本地化来自域类的错误消息。这是可能与默认的错误消息,例如:在Grails中本地化验证(错误)消息

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

和局部属性的名称,例如:

customer.address.label =客户地址

其中“客户”是我的域类,地址是它的属性。

我的问题是我无法本地化一些属性,因为我需要特定的错误消息。 E.g:

has.to.be.a.number =属性[{0}]必须是一个数

contingent.size.label =未定尺寸。

但我得到的消息是“物权【尺寸】必须是一个数字”,而不是“物权[附带大小]必须是一个数字”。

我不能定位的消息被以下:

  • 属性[{0}]必须是一个数
  • 属性[{0}]必须是一个有效的日期//我不能使用G:日期选择在此背景下


我添加了一些其他领域类的一些其他的例子也不起作用

package cz.quanti.spaportal.touristOffice 

import ... 

class TouristOffice { 
    String customerNumber 
    int minimalContingent 
    Address address 
    User user 
    ContactPerson contactPerson 

    static hasMany = [contingents: Contingent] 

    static constraints = { 
     customerNumber(unique:true, nullable: true, blank: true) 
     user(nullable: true, blank: true) 
     contactPerson(nullable: false) 
     minimalContingent(min: 0) 
     address(nullable: false) 
    } 

只有“minimalContingent”未本地化:(消息已本地化且最小属性未定义) 属性[minimalContingent]必须是一个数字。

+0

你正在使用什么验证器?请添加您的域类代码约束。 – mfloryan 2011-01-12 16:05:57

+0

您的域类是否在包中,或者它们是否使用默认包? – 2011-01-12 16:10:38

+0

此外,您是否遇到了使标签正常工作或使自定义验证消息起作用的问题?我假设前者,但是在重新阅读你的问题之后,我现在对你遇到的问题更加模糊。 – 2011-01-12 16:18:32

回答

1

我认为该属性的整个路径(最后没有“标签”)应该适合你。看起来像这样:

com.example.Customer.homeAddress=Customer address 

记得在需要它们的地方使用小写和大写!

2

确保您在定义中使用了域类包。同时检查你的大小写;我不知道这是否有差别,但我的成功messages.properties使用标签已经看过类似如下:

// messages.properties 
com.example.Customer.address.label=Customer address 
com.example.Contingent.size.label=Contingent size 

// or if you're using the default package 
Customer.address.label=Customer address 
... 


您的更新后,你能澄清什么?你有你的 messages.properties如下:

cz.quanti.spaportal.touristOffice.TouristOffice.minimalContingent.label=... 

如果不是,它如果添加它的工作?

4

如果您的验证消息有问题,则可以始终使用实例上的errors集合来检查验证错误的代码。

Customer c = ... 

c.validate() 

c.errors.each { println it } 
c.errors.getFieldError("address").codes.each { println it } 
c.errors.getFieldError("address").defaultMessage 

编写一个单元测试以检查代码以本地化消息。