如果将 Wicket 表单组件设置为“必需”,是否可以动态更改它?

发布于 2024-10-02 04:45:44 字数 2330 浏览 3 评论 0原文

编辑:
这个问题最初是关于复选框的,但我在下拉列表中得到了相同的行为。代码:

productInput = new DropDownChoice<String>("productInput",
                                          new PropertyModel<String>(this, "productSelection"),
                                          products);

productInput.add(new AjaxFormComponentUpdatingBehavior("onchange") {
    @Override
    protected void onUpdate(AjaxRequestTarget target)
    {
        if (productSelection == null) // Breakpoint is set on this line
        {
            updateDropdownsAfterFieldDisabled(1, target);
        }
        else
        {
            updateDropdownsAfterFieldEnabled(1, target);
        }
    }
});

如果 productInput 未设置为必需,则每次列表从选择某个值到选择空行选项时都会命中断点。如果将其设置为必需,则永远不会命中断点。


如果使用 setRequired(true); 进行验证,是否可以动态/AJAX 方式更改 Wicket 表单组件?这是一个简单的(如果是人为的)示例来说明我的意思:

加油站的免下车洗车通常通常提供三到四个级别的服务。每一个都包括较低级别提供的所有内容,并再涂上一层蜡或额外的冲洗或其他东西。清洗用户界面通常有四个按钮,旁边有指示灯 (示例)。当用户按下其中一个按钮时,相应的灯以及较便宜级别的所有灯都会亮起;达到更好水平的灯会关闭。这种行为是由这段代码建模的:

boolean economy, standard, deluxe, ultimate = false;

CheckBox economyBox = new CheckBox("economyBox",
                                   new PropertyModel<Boolean>(this, "economy"));
// Similar declarations for standardBox, deluxeBox and ultimatebox

OnChangeAjaxBehavior standardChangeListener = new OnChangeAjaxBehavior() {
    protected void onUpdate(AjaxRequestTarget target)
    {
        // If "standard" is activated, also turn on the "economy" light
        if (standard)
        {
            economy = true;
            target.addComponent(economyBox);
        }
        // If "standard" is deactivated, deactivate "deluxe" and "ultimate"
        else if (!standard)
        {
            deluxe = false;
            ultimate = false;
            target.addComponent(deluxeBox);
            target.addComponent(ultimateBox);
        }
    }
};
standardBox.add(standardChangeListener);
// Similar listeners declared for the other three checkboxes

我已经测试了这段代码——嗯,这是一个简化版本的真实代码——并且它按预期工作。但添加 economyBox.setRequired(true); 后,该框将停止动态更新。有没有办法在不破坏“链接复选框”行为的情况下添加验证?

EDIT:
This question was originally about checkboxes, but I am getting the same behavior with a dropdown list. The code:

productInput = new DropDownChoice<String>("productInput",
                                          new PropertyModel<String>(this, "productSelection"),
                                          products);

productInput.add(new AjaxFormComponentUpdatingBehavior("onchange") {
    @Override
    protected void onUpdate(AjaxRequestTarget target)
    {
        if (productSelection == null) // Breakpoint is set on this line
        {
            updateDropdownsAfterFieldDisabled(1, target);
        }
        else
        {
            updateDropdownsAfterFieldEnabled(1, target);
        }
    }
});

If productInput is not set to required, the breakpoint gets hit every time the list goes from having some value selected to having the blank line option selected. If it is set to required, the breakpoint never gets hit.


Is it possible to dynamically/AJAX-ishly change a Wicket form component if it's being validated with setRequired(true);? Here's a simple, if contrived, example to show what I mean:

Drive-through carwashes at gas stations usually often three or four levels of service. Each one includes everything provided by the lower levels and tacks on one more coat of wax or an extra rinse or something. UIs for the washes usually have four buttons with lights next to them (example). When a user presses one of the buttons, the corresponding light turns on, along with all the lights of the cheaper levels; the lights for nicer levels turn off. That behavior is modeled by this code:

boolean economy, standard, deluxe, ultimate = false;

CheckBox economyBox = new CheckBox("economyBox",
                                   new PropertyModel<Boolean>(this, "economy"));
// Similar declarations for standardBox, deluxeBox and ultimatebox

OnChangeAjaxBehavior standardChangeListener = new OnChangeAjaxBehavior() {
    protected void onUpdate(AjaxRequestTarget target)
    {
        // If "standard" is activated, also turn on the "economy" light
        if (standard)
        {
            economy = true;
            target.addComponent(economyBox);
        }
        // If "standard" is deactivated, deactivate "deluxe" and "ultimate"
        else if (!standard)
        {
            deluxe = false;
            ultimate = false;
            target.addComponent(deluxeBox);
            target.addComponent(ultimateBox);
        }
    }
};
standardBox.add(standardChangeListener);
// Similar listeners declared for the other three checkboxes

I've tested this code — well, the real code that this is a simplified version of — and it works as expected. But add in economyBox.setRequired(true); and the box stops updating dynamically. Is there a way to add validation without breaking the "linked checkbox" behavior?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

荆棘i 2024-10-09 04:45:44

我在这里看不出有什么问题,除非你有

        target.addComponent(standardBox);

我认为你想要的

        target.addComponent(economyBox);

验证应该与 OnChangeAjaxBehavior 互操作就可以了。我很确定我已经在某些形式中使用过这两种形式,尽管我认为没有使用复选框。

但我不确定使用 economyBox.setRequired(true); 进行验证的想法在这里是否有意义。您在模型中提供值,所有复选框默认为 false,因此在这种情况下没有真正的方法可以实际省略任何内容。

更明智的验证可能是必须检查某些内容,但这需要为整个表单或包含所有复选框的FormComponent定义不同的验证器。

编辑:
根据您关于使用 DropDownChoice 看到相同问题的编辑,我做了一个实验。

这里有一个带有 DropDownChoice 组件的 AjaxFormComponentUpdatingBehavior 示例< /a>.我为此下载了代码,添加了

    models.setRequired(true);
    makes.setRequired(true);
    add(new FeedbackPanel("messages"));

反馈面板,当然还添加了标记。

它验证了必填字段,并根据 make 下拉列表的值继续更新模型下拉列表。

由于这对我来说效果很好,我建议您尝试相同的方法,看看它是否有效,然后与您的代码进行比较。

I can't see anything wrong here except you have

        target.addComponent(standardBox);

where I think you intended

        target.addComponent(economyBox);

Validation should interoperate with OnChangeAjaxBehavior just fine. I'm quite sure I've used both in some forms, though not I think with checkboxes.

I'm not sure the idea of validation with economyBox.setRequired(true); makes sense here though. You're supplying values, defaulting to false for all the checkboxes, in your model, so there's no real way for anything to be actually omitted in this context.

A more sensible validation might be that something has to be checked, but that would require a different validator defined for the form as a whole, or for a FormComponent incorporating all your checkboxes.

EDIT:
Based on your edit about seeing the same issue with DropDownChoice, I did an experiment.

There is an example of AjaxFormComponentUpdatingBehavior with DropDownChoice components here. I downloaded the code for this, added

    models.setRequired(true);
    makes.setRequired(true);
    add(new FeedbackPanel("messages"));

and of course added markup for the feedback panel.

It validated the required fields and continued to update the model drop-down based on the value of the make dropdown.

Since this worked fine for me, I'd recommend you try the same, see it work and then compare with your code.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文