2012-04-25 49 views
5

我有一個檢票文本字段包含一個整數值如何檢查Wicket文本字段中的空值?

currentValueTextField = new TextField<IntParameter>("valueText", new PropertyModel<IntParameter>(model, "value")); 

我附加自定義驗證此,如下

currentValueTextField.add(new IntegerValidator()); 

驗證器類是

class IntegerValidator extends AbstractValidator<IntParameter> { 

private static final long serialVersionUID = 5899174401360212883L; 

public IntegerValidator() { 
} 

@Override 
public void onValidate(IValidatable<IntParameter> validatable) { 
    ValidationError error = new ValidationError(); 
    if (model.getValue() == null) { 
     AttributeAppender redOutline = new AttributeAppender("style", new Model<String>("border-style:solid; border-color:#f86b5c; border-width: 3px"), ";"); 
     currentValueTextField.add(redOutline); 
     currentValueTextField.getParent().getParent().add(redOutline); 
     validatable.error(error); 
     } 
    } 
} 

然而如果我在文本字段中沒有輸入任何內容,則我的onValidate()方法未被調用。

在這種情況下檢查空值的建議方法是什麼? 我還想對輸入的值進行範圍檢查。

+0

我不知道檢票,但在其他Web框架,我知道驗證不叫空文本字段。有些提供標籤來標記他們的標籤以啓用/禁用所需的檢查,而在其他情況下,當將表單值放入模型(或之後檢查模型)時,您可能必須執行檢查。 – Thomas 2012-04-25 14:38:35

回答

3

覆蓋validateOnNullValue()默認爲false

@Override 
public boolean validateOnNullValue() 
{ 
    return true; 
} 

這是validateOnNullValue()方法的描述:

指示是否要驗證的值,如果它是null。如果值爲null,通常需要跳過驗證,除非我們要確保 的值實際上是null(罕見用例)。驗證器,擴展此和 希望確保值爲null應覆蓋此方法並返回 true

+0

覆蓋validateOnNullValue()會導致我的驗證器被調用,但有趣的是我的模型中的值未設置爲null。所以我的驗證器代碼不會引發錯誤,因爲它認爲文本字段的值非空。 – 2012-04-25 15:21:34

+0

嗯......但是'null'必須在這裏'validatable.getValue()',當你傳遞驗證器時,該值仍然沒有在模型上設置。 – jordeu 2012-04-25 15:42:17

+0

謝謝! validatable.getValue()取得了訣竅。 – 2012-04-25 17:04:21

4

就叫

currentValueTextField.setRequired(true); 

的要求,以紀念場,並有檢票處理它自己的空值。您可以輕鬆地爲每個輸入字段組合多個驗證器。

任何特殊的錯誤處理,如添加紅色邊框或顯示錯誤消息,都可以在表單的onError方法中執行,或者將FeedbackBorder添加到相應的字段中。

+0

問題在於我無法控制如何指示錯誤。我想向文本框添加紅色邊框,但是wicket會在「反饋」面板中顯示錯誤消息。我不想使用反饋面板。 – 2012-04-25 16:13:57

+0

@AndrewFielden這可以通過爲表單實現onError()來存檔。我擴展了我的答案來涵蓋這一點。 – Nicktar 2012-04-26 09:12:08

+0

謝謝。編寫Wicket的人想到了一切。 – 2012-04-26 14:30:03

1

一個更好的(和可重複使用)的方式來做到這一點是重寫行爲isEnabled(Component)方法:

public class HomePage extends WebPage { 
    private Integer value; 
    public HomePage() { 
     add(new FeedbackPanel("feedback")); 
     add(new Form("form", new CompoundPropertyModel(this)) 
      .add(new TextField("value") 
       .setRequired(true) 
       .add(new ErrorDecorationBehavior())) 
      .add(new Button("submit") { 
       @Override 
       public void onSubmit() { 
        info(value.toString()); 
       } 
      })); 
    } 
} 

class ErrorDecorationBehavior extends AttributeAppender { 
    public ErrorDecorationBehavior() { 
     super("style", true, Model.of("border-style:solid; border-color:#f86b5c; border-width: 3px"), ","); 
    } 
    @Override 
    public boolean isEnabled(Component component) { 
     return super.isEnabled(component) && component.hasErrorMessage(); 
    } 
} 
2
currentValueTextField.setRequired(true); 

現在你需要自定義錯誤消息。所以子類FeedbackPanel。

,你可以找到在以下link

更多信息,此類添加到您的窗體或組件