2010-11-05 53 views
1

是否可以在Wicket中嵌套獨立於彼此的窗體?我想要一個帶有提交按鈕和取消按鈕的表單。兩個按鈕都應該引導用戶訪問相同的頁面(我們稱之爲Foo)。提交按鈕應該首先發送一些信息給服務器;取消按鈕應該什麼也不做。是否可以在Wicket中嵌套相互獨立的窗體?

這裏是我現有的代碼一個非常簡化的版本:

Form form = new Form() { 
    public void onSubmit() 
    { 
     PageParameters params = new PageParameters(); 
     params.put("DocumentID", docID); 
     setResponsePage(Foo.class, params); 
    } 
}; 

DropDownChoice<String> ddc = new DropDownChoice<String>("name", new PropertyModel<String>(this, "nameSelection"), names); 
ddc.setRequired(true); 

final Button submitButton = new Button("Submit") { 
    public void onSubmit() { doSubmitStuff(true); } 
}; 

final Button cancelButton = new Button("Cancel") { 
    public void onSubmit() { doSubmitStuff(false); } 
}; 

form.add(ddc); 
form.add(submitButton); 
form.add(cancelButton); 
form.add(new FeedbackPanel("validationMessages")); 

的問題是,我只是增加了一個驗證器,即使我按下取消按鈕,因爲取消按鈕連接到它觸發與其他一切相同的形式。如果取消按鈕是單獨的形式,這可以避免。據我所知,我不能創建一個單獨的窗體,因爲—由於HTML —的結構,單獨的窗體將在組件層次結構中的現有窗體下。

儘管有層次結構,我可以使表單以某種方式分開嗎?或者有其他解決方案可以使用嗎?

編輯:
針對唐羅比的評論,這是一個有點接近看上去像回到我的代碼時,我試圖setDefaultFormProcessing()

Form<Object> theForm = new Form<Object>("theForm") { 
     public void onSubmit() 
     { 
      PageParameters params = new PageParameters(); 
      params.put("DocumentID", docID); 
      setResponsePage(Foo.class, params); 
     } 
    }; 

    final CheckBox checkbox = new CheckBox("checkbox", new PropertyModel<Boolean>(this, "something")); 
    checkbox.add(new PermissionsValidator()); 
    theForm.add(checkbox); 

    final Button saveButton = new Button("Save") { 
     public void onSubmit() 
     { someMethod(true); } 
    }; 
    final Button cancelButton = new Button("Cancel") { 
     public void onSubmit() 
     { someMethod(false); } 
    }; 

    cancelButton.setDefaultFormProcessing(false); 
    theForm.add(saveButton); 
    theForm.add(cancelButton); 
    theForm.add(new FeedbackPanel("validationMessages")); 
+0

您似乎在新的示例代碼中有兩種形式(theForm和configureRuleForm)。這是編輯事故還是真的有兩種形式? – 2010-11-06 22:39:40

+0

@唐,對不起,編輯意外;它現在已經修復了。 – Pops 2010-11-06 22:46:04

回答

4

還有一個更簡單的解決辦法:撥打setDefaultFormProcessing取消按鈕法false作爲參數:

cancelButton.setDefaultFormProcessing(false); 

這種方式,點擊取消按鈕將繞過形式驗證(和模型更新),直接調用噸他onSubmit功能。

+1

-1,這是我嘗試的第一件事情之一,但驗證一直在解僱。 – Pops 2010-11-06 16:46:32

+0

@Lord Torgamus - 對不起,這似乎不適合你,但它確實是跳過驗證提交按鈕的正常方法。 – 2010-11-06 21:25:51

+0

好的,我已經試過了,現在看起來可行。一定有一些單獨的錯誤阻止了之前工作的頁面。投票從-1更改爲+1。 (抱歉編輯,但我不能改變我的選票。) – Pops 2010-11-06 23:25:05

2

這是可能的「嵌套」形式在檢票口。

請參閱this wiki entry 關於它如何工作的一些說明以及this wiki entry關於它如何與驗證進行交互的說明。

但是對於你所追求的,Jawher的答案本應該起作用並且簡單得多。

看看這個example code提示工作。

我想知道你是否在本文中簡化了你的代碼。你能製作一個小到足以發佈的樣本,肯定有問題嗎?

相關問題