如何为检票口中 'required' 属性 为真的所有组件添加自定义 css class?
How to add custom css class for all components where the 'required' property is true in wicket?
我在我的 wicket 应用程序中使用 jQuery 验证器插件(wicket 的版本是 8.10)。它使用 CSS 类 来标识应该应用于该字段的验证器。要根据需要标记组件,我使用 FormComponent#setRequired
方法。另外,我有自己的组件(继承自 wicket 组件),我在其中覆盖了 onComponentTag
方法,如下所示:
@Override
protected void onComponentTag(ComponentTag tag) {
if (isRequired()) {
tag.append("class", CSS.required(), " ");
}
super.onComponentTag(tag);
}
我还对本机 wicket 组件使用以下构造:
if (field.isRequired()) {
field.add(new AttributeAppender("class", CSS.required()));
}
有没有办法在不覆盖 onComponentTag 或为每个字段对象添加 AttributeAppender 的情况下为任何 FormComponent 设置此行为?任何全局 AttributeModifier 或类似的东西?
是的,有:
Application.get().getOnComponentTagListeners()
.add(new IOnComponentTagListener() {
@Override
public void onComponentTag(final Component component, final ComponentTag tag) {
if (component instanceOf FormComponent) {
if (((FormComponent) component).isRequired()) {
tag.append("class", CSS.required(), " ");
}
}
}
});
我在我的 wicket 应用程序中使用 jQuery 验证器插件(wicket 的版本是 8.10)。它使用 CSS 类 来标识应该应用于该字段的验证器。要根据需要标记组件,我使用 FormComponent#setRequired
方法。另外,我有自己的组件(继承自 wicket 组件),我在其中覆盖了 onComponentTag
方法,如下所示:
@Override
protected void onComponentTag(ComponentTag tag) {
if (isRequired()) {
tag.append("class", CSS.required(), " ");
}
super.onComponentTag(tag);
}
我还对本机 wicket 组件使用以下构造:
if (field.isRequired()) {
field.add(new AttributeAppender("class", CSS.required()));
}
有没有办法在不覆盖 onComponentTag 或为每个字段对象添加 AttributeAppender 的情况下为任何 FormComponent 设置此行为?任何全局 AttributeModifier 或类似的东西?
是的,有:
Application.get().getOnComponentTagListeners()
.add(new IOnComponentTagListener() {
@Override
public void onComponentTag(final Component component, final ComponentTag tag) {
if (component instanceOf FormComponent) {
if (((FormComponent) component).isRequired()) {
tag.append("class", CSS.required(), " ");
}
}
}
});