【问题标题】:Is it possible to nest forms in Wicket that are independent of each other?是否可以在 Wicket 中嵌套相互独立的表单?
【发布时间】: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,抱歉,编辑意外;现在已经修好了。

标签: java ajax wicket


【解决方案1】:

还有一个更简单的解决方案:在取消按钮上调用setDefaultFormProcessing 方法,并以false 作为参数:

cancelButton.setDefaultFormProcessing(false);

这样,点击取消按钮将绕过表单验证(和模型更新),直接调用onSubmit函数。

【讨论】:

  • -1,这是我尝试的第一件事,但验证一直在触发。
  • @Lord Torgamus - 抱歉,这似乎对您不起作用,但这确实是跳过提交按钮验证的正常方式。
  • 好的,我又试了一次,现在好像可以了。之前一定有一些单独的错误导致页面无法正常工作。投票从 -1 变为 +1。 (抱歉编辑,否则我无法更改我的投票。)
【解决方案2】:

可以在检票口中“嵌套”表单。

this wiki entry 有关它如何工作的一些说明,以及有关它如何与验证交互的this wiki entry

但对于您所追求的,Jawher 的回答应该有效,而且要简单得多。

查看example code 以获取有关使其正常工作的提示。

我想知道您是否在这篇文章中将代码简化得太远了。你能制作一个足够小的样本来发布肯定有问题的样本吗?

【讨论】:

    猜你喜欢
    • 2018-10-27
    • 1970-01-01
    • 2016-10-15
    • 1970-01-01
    • 2013-08-12
    • 2011-08-25
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    相关资源
    最近更新 更多