2012-07-27 76 views
1

我正在使用wickets 1.5.I有註冊頁面,其中fileds必須僅針對數字進行驗證(如電話號碼)。我有一個驗證類,如下在wicket中添加.properties文件

public class Validator implements IValidator<String> { 
     Pattern pattern; 
     public Validator() { 
     pattern = Pattern.compile("[0-9]+"); 
     } 

     public void validate(IValidatable<String> validatable) { 
     final String field = validatable.getValue(); 

     if (pattern.matcher(field).matches() == false) { 

      error(validatable, "phoneno"); 
     } 

     } 

    private void error(IValidatable<String> validatable, String errorKey) { 
    ValidationError error = new ValidationError(); 
    error.addMessageKey(getClass().getSimpleName() + "." + errorKey); 
    validatable.error(error); 
    } 

} 

我在同一個包Registation.html和.java文件是有我Registration.properites文件。 我Registration.properites

Registration.phoneno =請輸入數字只有

我在我的檢票類調用此
phoneno.add(新的驗證());

我正在下面誤差

未能找到部件錯誤消息:的TextField @ sendform:PHONENO和錯誤:[ValidationError消息= [零],鍵= [Validator.phoneno],變量= [空值]]。試過的鑰匙:phoneno.Validator.phoneno,Validator.phoneno。

我做錯了什麼? 我按照以下鏈接做了這個鏈接 http://www.mkyong.com/wicket/create-custom-validator-in-wicket/

回答

2

我想你沒有正確指定屬性鍵。當您的密鑰以'註冊'開始時,getClass().getSimpleName()方法返回「驗證器」。試試這個:

error.addMessageKey("Registration.phoneno"); 
相關問題