2014-02-23 42 views
3

我寫一個Grails(目前2.3.3)應用程序並創建了一個validateable命令自定義錯誤消息的對象類似以下內容:Grails的定義命令對象

@Validateable 
class MyCustomCommand { 
    String name 

    static constraints = { 
    name blank: false 
    } 
} 

在我i18n/messages.properties文件I定義了以下屬性來覆蓋默認的錯誤消息。

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. 

我假設我在這裏錯過了一些簡單的東西,但尚未偶然發現什麼。任何建議,我在做什麼不正確?

回答

2

確實很簡單。信息應該看起來像:

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) 
+0

我相信這是第一個變化我試過了,我剛纔試了一下,還是把默認。 – Michael

+0

哎呀我錯過了零件,而關於所有關於小寫的情況。糾正。 :) – dmahapatro

1

所以,正如我預料的那樣,它是簡單的。我使用的默認值爲null,其中我真正需要的是nullable。這與合適的約束名稱相匹配是有意義的。

因此正確的版本是:

myCustomCommand.name.blank=Name must be provided. 
myCustomCommand.name.nullable=Name must be provided.