编辑:
这个问题最初是关于复选框的,但我在下拉列表中得到了相同的行为。代码:
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
设置为必需,则每次列表从选择了某个值到选择空行选项时,都会触发断点。如果设置为required,则断点永远不会被击中。
如果使用setRequired(true);
验证Wicket表单组件,是否可以动态/ AJAX更改?这是一个简单的,如果有人工作的例子来说明我的意思:
加油站的直通式洗车通常经常提供三到四级服务。每一个都包括下层提供的所有东西,另外还有一层蜡或额外的冲洗或其他东西。洗涤的UI通常有四个按钮,旁边有灯光(example)。当用户按下其中一个按钮时,相应的灯会亮起,并且所有灯都会更便宜;更好的水平的灯关闭。该行为由此代码建模:
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);
,该框会动态停止更新。有没有办法在不破坏“链接复选框”行为的情况下添加验证?
答案 0 :(得分:0)
除了你有
,我在这里看不到任何错误 target.addComponent(standardBox);
我认为你打算
target.addComponent(economyBox);
验证应与OnChangeAjaxBehavior
互操作。我很确定我已经在某些形式中使用了两种形式,但我认为不是用复选框。
我不确定使用economyBox.setRequired(true);
进行验证的想法在这里有意义。您在模型中为所有复选框提供值,默认为false,因此在此上下文中没有任何实际省略的方法。
更明智的验证可能是必须检查某些内容,但这需要为整个表单定义不同的验证程序,或者需要包含所有复选框的FormComponent
修改强>
根据您对DropDownChoice
查看同一问题的修改,我做了一个实验。
有AjaxFormComponentUpdatingBehavior
个DropDownChoice
个组件here的示例。我为此下载了代码,添加了
models.setRequired(true);
makes.setRequired(true);
add(new FeedbackPanel("messages"));
当然还为反馈小组添加了标记。
验证了必填字段,并根据make下拉列表的值继续更新模型下拉列表。
由于这对我来说很好,我建议您尝试相同的操作,看看它是否正常工作,然后与您的代码进行比较。