2013-06-11 57 views
2

我有一個下拉框和一個TextField。文本字段的值必須以唯一編號開頭,具體取決於所選內容。如果不顯示錯誤消息。錯誤消息中的變量

根據選擇的錯誤信息是這樣的:

在「文本字段」的數量必須以101開頭的選擇1

在「文本字段」的個數必須開始與102,用於選擇2

我已經寫自定義驗證來驗證的TextField。

我不喜歡的是,對於每個錯誤我有其相應價值的錯誤信息資源的關鍵:在屬性文件

@Override 
protected void onValidate(IValidatable<String> validatable) { 

    if(selection.equals(selections1) && !beginsWithGoodNumber){ 
     error(validatable, "error.selection1"); 
    }else if(selection.equals(selections1) && !beginsWithGoodNumber){ 
     error(validatable, "error.selection2"); 
    }else if(selection.equals(selections1) && !beginsWithGoodNumber){ 
     error(validatable, "error.selection3"); 
    } 
} 

我:

error.selection1 = the number in the '${label}' must begin with 101 for selection1 
error.selection2 = the number in the '${label}' must begin with 102 for selection2 
error.selection3 = the number in the '${label}' must begin with 103 for selection3 
error.selection4 = the number in the '${label}' must begin with 104 for selection4 

我想在屬性文件中有這樣的東西:

error.selection = the number in the '${label}' must begin with {number} for {selection} 

其中{number}{selection}是變量。

這可能嗎?

謝謝。

回答

1

假設您的驗證器擴展了AbstractValidator,您可以將變量映射傳遞給#error()。

或者參見AbstractRangeValidator關於如何創建ValidationError,在其上設置變量並將其報告給組件。

0

您可以設置ValidationError對象並將其設置爲validatable.error方法。 ValidationError有一個方法來設置變量。