2013-02-27 27 views
1

我有一個表單,其輸入在某些組合中是必需的,也取決於哪個提交按鈕被點擊。檢票口:有條件的驗證器

E.g.字段A,B,和C,和提交按鈕男,N。和有效的組合是 L:A + B N:A + B + C N:阿 Ñ:C

所以沒有的輸入是始終需要。

目前我解決它使用if(...)在onSubmit(),但我想知道:我可以移動這些檢查到每個組件的驗證器與回調(dis)批准某種組合?

更新:

_______________________ 

    User name: ______ 
    Password: ______ 

    [ Log in button ] 

    Email:  ______ 

    [ Register/reset password button ] 
_______________________ 

登錄需要用戶名和密碼。 註冊需要全部註冊,在這種情況下,註冊, 或只是郵件,或只是用戶名,在這種情況下,它會發送傳遞重置質詢郵件。

+1

難道你看看IFormValidator?它允許你指定依賴關係。但是它並不能解決驗證因單擊按鈕而不同的問題。 – 2013-02-27 12:53:38

+0

我想重用現有的驗證器,但根據這些條件「激活」它們。也許我可以將它們鏈接到我的impl後面。 – 2013-02-28 09:23:02

回答

1

我建議你添加AjaxFormComponentUpdatingBehaviors到您的組件,然後禁用當前組合通過

formComponent.setEnabled(false); 
target.add(formComponent); 

禁用的組件不會驗證被認爲是不應該提供所有組件。另一個好處是,用戶總是立即對允許的組合有反饋。

+0

+1,但不幸的是,這不是我的情況的選項,請參閱更新的問題。 – 2013-03-05 19:04:29

2

https://cwiki.apache.org/WICKET/conditional-validation.html看一看的例子在,它應該差不多解決您的問題:

Button submit = new Button("submit") { 
    public void onSubmit() { 
     // handle form submission 
    } 
} 
form.add(submit); 

TextField foo = new TextField("foo") { 
    public boolean isRequired() { 
     Form form = (Form) findParent(Form.class); 
     return form.getRootForm().findSubmittingButton() == button; 
    } 
} 
form.add(foo); 

讓我知道如果你需要任何進一步的幫助,或者如果它不工作。乾杯

+0

這看起來不錯!要嘗試 – 2013-03-06 18:49:25

+0

如果能解決這個問題,你能否將這個答案標記爲已解決?也會幫助我。謝謝。 – Daniel 2013-03-25 08:35:36

0

假設的形式看起來像這樣:

TextField user = new TextField(); 
user.setRequired(true); 
TextField pass = new TextField(); 
TextField email = new TextField(); 
Button login = new Button(); 
Button register = new Button(); 

我會嘗試創建一個FormValidator類,像這樣:

public class MyValidator extends AbstractFormValidator 
{ 
    private FormComponent[3] fields; 

    public MyValidtor(TextField field1, TextField, field2, Button button) 
    { 
     fields = {field1, field2, button }; 
    } 

    //getDependentFormComponents returns fields; 

    public void validate(Form form) 
    { 
     if (form.getRootForm().findSubmittingButton() == button) 
     { 
     //do field1 & field2 required checks here 
     } 
    } 
} 

然後只需添加MyValidator到窗體

form.add(new MyValidator(user, pass, login)); 
form.add(new MyValidator(user, email, register));