0

I have a checkbox and dropdown in a html page . On click of submit if both of them are selected , their should be a error message that any one of them should be mandatory field and asked to select one value atleast.

I created a new class as as FormValidator implementing IValidator and override the validate method. But i am unaware how to use the dropdown choice value and checkbox value on submit method call ad how to display in html .

Java code -

IModel<Project> dropdownModel =   new PropertyModel<Project>(criteria,"selectedBillableProject");
        DropDownChoice<Project> billableProjectsList = new DropDownChoice<>(
                "projectsList",
                dropdownModel,
                billableProjects,
                new ChoiceRenderer<Project>("fullNameWithCustomer"));
        billableProjectsList.setLabel(new ResourceModel("printMonth.billable"));
        
        form.add(billableProjectsList);

}

protected void onSubmit() {
           .....
            validateInputComponents(reportCriteria);
.....
}

public void validateInputComponents(ReportCriteria reportCriteria){

          add(new FormComponentValidator(reportCriteria.getSelectedBillableProject(), unbillableProjectsView.getModelObject()));      

        }

I have updated the FormComponentValidtor similar to the :https://cwiki.apache.org/confluence/display/WICKET/Validating+related+fields

But its resulting in below error onSubmit (). 11-26@22:37:25 ERROR org.apache.wicket.request.cycle.RequestCycle - Error during processing error message java.lang.IllegalStateException: STREAM at org.eclipse.jetty.server.Response.getWriter(Response.java:693) at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:152) at javax.servlet.ServletResponseWrapper.getWriter(ServletResponseWrapper.java:152) at org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper.getWriter(SaveContextOnUpdateOrErrorResponseWrapper.java:125) at org.apache.wicket.protocol.http.servlet.ServletWebResponse.write(ServletWebResponse.java:109) at org.apache.wicket.protocol.http.HeaderBufferingWebResponse.write(HeaderBufferingWebResponse.java:179) at org.apache.wicket.protocol.http.BufferedWebResponse$WriteCharSequenceAction.invoke(BufferedWebResponse.java:171) at org.apache.wicket.protocol.http.BufferedWebResponse.writeTo(BufferedWebResponse.java:602) at org.apache.wicket.request.handler.render.WebPageRenderer.respond(WebPageRenderer.java:232) at org.apache.wicket.core.request.handler.RenderPageRequestHandler.respond(RenderPageRequestHandler.java:175) at org.apache.wicket.request.cycle.RequestCycle$HandlerExecutor.respond(RequestCycle.java:890) at org.apache.wicket.request.RequestHandlerStack.execute(RequestHandlerStack.java:64) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:310) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:319) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:319) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:319) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:319) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:319) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:319) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:319) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:319) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:319) at org.apache.wicket.request.cycle.RequestCycle.executeExceptionRequestHandler(RequestCycle.java:319) at org.apache.wicket.request.cycle.RequestCycle.processRequest(RequestCycle.java:233) at org.apache.wicket.request.cycle.RequestCycle.processRequestAndDetach(RequestCycle.java:289) at org.apache.wicket.protocol.http.WicketFilter.processRequestCycle(WicketFilter.java:259) at org.apache.wicket.protocol.http.WicketFilter.processRequest(WicketFilter.java:201) at org.apache.wicket.protocol.http.WicketFilter.doFilter(WicketFilter.java:282) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1484) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330) at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87) at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342) at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192) at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160) at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1484) at org.springframework.orm.hibernate4.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:151) at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107) at org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1476) at org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:501) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137) at org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:557) at org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231) at org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java:1086) at org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:429) at org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193) at org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1020) at org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135) at org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255) at org.eclipse.jetty.server.handler.HandlerCollection.handle(HandlerCollection.java:154) at org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116) at org.eclipse.jetty.server.Server.handle(Server.java:370) at org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:494) at org.eclipse.jetty.server.AbstractHttpConnection.content(AbstractHttpConnection.java:982) at org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.content(AbstractHttpConnection.java:1043) at org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:865) at org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:240) at org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82) at org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:696) at org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:53) at org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608) at org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543) at java.lang.Thread.run(Thread.java:748)

Am i missing something . Please help

sharonm
  • 75
  • 6
  • 1
    Please provide some code. – Or Assayag Nov 24 '20 at 15:21
  • @OrAssayag - updated the code – sharonm Nov 24 '20 at 15:56
  • The issue is fixed and i am able to display the error message as per the request . But one thing is once i select the valid inputs and submit , the feedback panel errror message still shows up . Any suggestion on how to clear the messages – sharonm Dec 01 '20 at 12:20
  • @OrAssayag - the feedback message displayed is retained in the page even after valid inout form submission . I tried SelectionForm.getFeedbackMessages() , component.getFeedbackMessages() , everything is empty . Unable to clear them . Please let me know any inputs .. trying from longtime – sharonm Dec 04 '20 at 16:34

1 Answers1

2

looks like your are missing a FeedbackPanel in your form to display validation errors/messages. Take a look at the user guide here and follow the entire example:

https://ci.apache.org/projects/wicket/guide/8.x/single.html#_form_validation_and_feedback_messages

Andrea Del Bene
  • 2,521
  • 1
  • 15
  • 20
  • i added the feedback
    in the html page . it displays the error . But i want it to display custom error message and has to display only if both checkbox and dropdown are empty .
    – sharonm Nov 25 '20 at 12:54
  • Look at the live example here for a complete use case of a custom validator and message: https://wicket-guide.herokuapp.com/customvalidator/?0 Then you can use a IFormValidator to handle multiple input fields. – Andrea Del Bene Nov 25 '20 at 14:37
  • can i add the custom validation using IFormValidator on the method - OnSubmit() – sharonm Nov 25 '20 at 16:20
  • Please help with some inputs on how to add validation for multiple components (Dropdown & Checkbox) onsubmit() – sharonm Nov 25 '20 at 16:28
  • i am able to retreive the dropdown and checkbox values onSubmit() method , but not sure how to use the validator to add the custom error message . Could you please help – sharonm Nov 26 '20 at 10:48
  • Maybe this old example from wiki page is what you need: https://cwiki.apache.org/confluence/display/WICKET/Validating+related+fields – Andrea Del Bene Nov 26 '20 at 16:39
  • - I followed the link and tried to use IFormValidator . Please let me know how to convert the CheckBox to FormComponent values , since its requierd for validation – sharonm Nov 27 '20 at 15:04
  • the feedback message displayed is retained in the page even after valid inout form submission . I tried SelectionForm.getFeedbackMessages() , component.getFeedbackMessages() , everything is empty . Unable to clear them . Please let me know any inputs .. trying from longtime – sharonm Dec 04 '20 at 16:33