2011-01-12 69 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 

編寫一個單元測試以檢查代碼以本地化消息。