title: How to validate the maximum amount of checked values of a selectManyCheckbox based on the current selection of a selectOneMenu?
body: <p>I am using JSF 2.0 and PrimeFaces. I have a selectOneMenu and a selectManyCheckbox. I want to achieve this scenario:</p>
  
  <p>When an user chooses:</p>
  
  <ul>
  <li>option1(PieChart), then the user is allowed to check max one checkbox</li>
  <li>option2(BarChart), then the user is allowed to check max two checkboxes</li>
  </ul>
  
  <p>Here is my attempt so far, the view:</p>
  
  <pre class="lang-xml prettyprint-override"><code>&lt;h:selectOneMenu value="#{bean.chartType}"&gt;
      &lt;f:selectItem itemValue="PieChart" itemLabel="PieChart" /&gt;
      &lt;f:selectItem itemValue="BarChart" itemLabel="BarChart" /&gt;
      &lt;p:ajax event="keyup" update="checkbox" listener="#{bean.checkboxHandler}" /&gt;
  &lt;/h:selectOneMenu&gt;
  
  &lt;h:selectManyCheckbox id="checkbox" value="#{bean.selectedItemscheckbox}"&gt;
      &lt;f:selectItem itemValue="priority" itemLabel="By priority" /&gt;
      &lt;f:selectItem itemValue="project" itemLabel="By project" /&gt;
      &lt;f:selectItem itemValue="issuetype" itemLabel="By IssueType" /&gt;
      &lt;f:selectItem itemValue="group" itemLabel="By group" /&gt;
  &lt;/h:selectManyCheckbox&gt;
  </code></pre>
  
  <p><em>(note: I am not sure if the <code>&lt;p:ajax event&gt;</code> is correct)</em></p>
  
  <p>The bean:</p>
  
  <pre><code>public void checkboxHandler() {
      Iterator it1 = selectedItemscheckbox.iterator();
      if (chartType.equals("PieChart")) {
          while (it1.hasNext()) {
              if (selectedItemscheckbox.size() &gt; 1) {
                  // Here should be somthing that shows a message to the user.
              }
          }
      }
  }
  </code></pre>
  
  <p>After tring BalusC's solution,this error occured :</p>
  
  <pre><code>java.lang.InstantiationException: DAOKPI.TestAjax$MyCheckboxValidator
  at java.lang.Class.newInstance0(Class.java:340)
  at java.lang.Class.newInstance(Class.java:308)
  at com.sun.faces.application.ApplicationImpl.newThing(ApplicationImpl.java:1699)
  at com.sun.faces.application.ApplicationImpl.createValidator(ApplicationImpl.java:1557)
  at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.createValidator(ValidatorTag     HandlerDelegateImpl.java:238)
  at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.applyAttachedObject(Validato    rTagHandlerDelegateImpl.java:132)
  at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.applyNested(ValidatorTagHand    lerDelegateImpl.java:205)
  at com.sun.faces.facelets.tag.jsf.ValidatorTagHandlerDelegateImpl.apply(ValidatorTagHandlerDel    egateImpl.java:87)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
  at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandle     r.java:137)
  at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDel    egateImpl.java:188)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
  at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
  at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
  at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
  at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
  at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
  at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.applyNextHandler(DelegatingMetaTagHandler.java:137)
  at com.sun.faces.facelets.tag.jsf.ComponentTagHandlerDelegateImpl.apply(ComponentTagHandlerDelegateImpl.java:188)
  at javax.faces.view.facelets.DelegatingMetaTagHandler.apply(DelegatingMetaTagHandler.java:120)
  at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
  at com.sun.faces.facelets.compiler.NamespaceHandler.apply(NamespaceHandler.java:93)
  at javax.faces.view.facelets.CompositeFaceletHandler.apply(CompositeFaceletHandler.java:98)
  at com.sun.faces.facelets.compiler.EncodingHandler.apply(EncodingHandler.java:86)
  at com.sun.faces.facelets.impl.DefaultFacelet.apply(DefaultFacelet.java:152)
  at com.sun.faces.application.view.FaceletViewHandlingStrategy.buildView(FaceletViewHandlingStrategy.java:769)
  at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:100)
  at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
  at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:139)
  at javax.faces.webapp.FacesServlet.service(FacesServlet.java:410)
  at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1534)
  at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:281)
  at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
  at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:655)
  at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:595)
  at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:98)
  at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:91)
  at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:162)
  at org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:326)
  at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:227)
  at com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:170)
  at com.sun.grizzly.http.ProcessorTask.invokeAdapter(ProcessorTask.java:822)
  at com.sun.grizzly.http.ProcessorTask.doProcess(ProcessorTask.java:719)
  at com.sun.grizzly.http.ProcessorTask.process(ProcessorTask.java:1013)
  at com.sun.grizzly.http.DefaultProtocolFilter.execute(DefaultProtocolFilter.java:225)
  at com.sun.grizzly.DefaultProtocolChain.executeProtocolFilter(DefaultProtocolChain.java:137)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:104)
  at com.sun.grizzly.DefaultProtocolChain.execute(DefaultProtocolChain.java:90)
  at com.sun.grizzly.http.HttpProtocolChain.execute(HttpProtocolChain.java:79)
  at com.sun.grizzly.ProtocolChainContextTask.doCall(ProtocolChainContextTask.java:54)
  at com.sun.grizzly.SelectionKeyContextTask.call(SelectionKeyContextTask.java:59)
  at com.sun.grizzly.ContextTask.run(ContextTask.java:71)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:532)
  at com.sun.grizzly.util.AbstractThreadPool$Worker.run(AbstractThreadPool.java:513)
  at java.lang.Thread.run(Thread.java:662)
  </code></pre>
  
  <p>Update</p>
  
  <p>Thank you problem solved :) </p>