0
votes

In Wicket when I disable a Form instance it perfectly disables my input fields, but it fails to disable the submit button, too.

How to disable the submit button, too, preferable without the need to add wicket:id for the submit button?

2
Why do you not want to use wicket:id? - idontevenseethecode
Because I then have to use it for every form. I'd rather want it to work automatically - I rather consider it a bug in Wicket, that it does not disable it automatically. - Thomas S.
Disabling the form disables all child components as well. But without wicket:id a Wicket Component can never be created for the submit button, it just stays plain HTML. - idontevenseethecode

2 Answers

1
votes

Use an AjaxSubmitLink and add it to the form. It will be enabled/disabled along with the form:

HTML:

<button wicket:id="submit">Submit</button>

Java:

form.add(new AjaxSubmitLink("submit"));
0
votes

I've solved this problem by automatically created Buttons. In my WebApplication's init()-method I install an AbstractMarkupFilter that makes <input type="submit"/> autolinks:

markupSettings.setMarkupParserFactory(new IMarkupParserFactory() {
    public MarkupParser newMarkupParser(MarkupResourceStream resource) {
        final MarkupParser parser = new MarkupParser(new XmlPullParser(), resource);
        parser.appendMarkupFilter(new AbstractMarkupFilter() {
            public MarkupElement nextTag() throws ParseException {
                final ComponentTag tag = (ComponentTag)getParent().nextTag();
                if (tag == null) {
                    return null;
                }

                if (tag.getId() != null) {
                    return tag;
                }

                if (!tag.isOpen() && !tag.isOpenClose()) {
                    return tag;
                }

                if (tag.getName().equals("input") && !(tag instanceof WicketTag)) {
                    final String type = tag.getAttributes().getString("type");
                    if ("submit".equals(type)) {
                        tag.enableAutolink(true);
                        tag.setId(WicketLinkTagHandler.AUTOLINK_ID);
                        tag.setAutoComponentTag(true);
                        tag.setModified(true);
                    }
                }
                return tag;
            }
        };
        return parser;
    }
});

Then I install an IComponentResolver before the existing AutoLinkResolver:

final List<IComponentResolver> componentResolvers = getPageSettings().getComponentResolvers();
for (int i ; i < componentResolvers.size(); i++) {
    final IComponentResolver resolver = componentResolvers.get(i);
    if (resolver instanceof AutoLinkResolver) {
        componentResolvers.add(i, new IComponentResolver() {
            @Override
            public boolean resolve(MarkupContainer container, MarkupStream markupStream, ComponentTag tag) {
                if (tag.isAutolinkEnabled()) {
                    if (tag.getName().equals("input")) {
                        final String type = tag.getAttribute("type");
                        if ("submit".equals(type)) {
                            final Page page = container.getPage();
                            final String autoId = WicketLinkTagHandler.AUTOLINK_ID + Integer.toString(page.getAutoIndex());
                            if (tag.getId() == null) {
                                tag.setId(autoId);
                                tag.setAutoComponentTag(true);
                            }
                            container.autoAdd(new Button(autoId));
                            return true;
                        }
                    }
                }

                return false;
            }
        });
        break;
    }
}