【发布时间】:2010-11-05 19:56:34
【问题描述】:
是否可以在 Wicket 中嵌套相互独立的表单?我想要一个带有提交按钮和取消按钮的表单。两个按钮都应该将用户引导到同一个页面(我们称之为 Foo)。提交按钮应该首先向服务器发送一些信息;取消按钮应该什么都不做。
这是我现有代码的真正简化版本:
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 的结构——单独的表单将位于组件层次结构中的现有表单之下。
我可以让表单以某种方式分开,尽管有层次结构吗?或者我可以使用其他解决方案吗?
编辑:
作为对 Don Roby 评论的回应,这更接近于我在尝试 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"));
【问题讨论】:
-
您在新的示例代码中似乎有两个表单(theForm 和 configureRuleForm)。这是编辑事故还是真的有两种形式?
-
@Don,抱歉,编辑意外;现在已经修好了。