1 Reply Latest reply on Aug 31, 2009 10:08 AM by ilya_shaikovsky

    validation of dates selected in rich:calendar

    k_krish_rao

      I have a rich calendar implemented using a CalendarDataModel. A modal panel is displayed when I select a date to mark it as a holiday or a recurring holiday.

      <h:form id="calendarform">
       <a4j:outputPanel id="forceELFunctionSerialization" ajaxRendered="true" style="display: none">
      
       <a4j:jsFunction name="ajaxSubmit" oncomplete="#{rich:component('panel')}.show()" reRender="editContent" />
       </a4j:outputPanel>
       <rich:panel header="Test Calendar Data Model">
       <rich:calendar id="businessCalendar" value="#{calendarBean.selectedDate}" timeZone="#{calendarBean.timeZone}" datePattern="#{calendarBean.datePattern}" showApplyButton="#{calendarBean.showApply}" popup="#{calendarBean.popup}" enableManualInput="#{calendarBean.manualInput}" dataModel="#{calendarDataModel}" showWeeksBar="false" mode="ajax" valueChangeListener="#{calendarDataModel.valueChanged}" onchanged="if (event.rich.date) {ajaxSubmit();}" cellWidth="50px" cellHeight="50px" validator="#{calendarBean.validateDate}" >
       </rich:calendar>
       <a4j:commandButton value="Get Date" action="#{calendarBean.printDate}" />
       <h:inputHidden value="message" id="errorMessage"/>
       <h:message for="errorMessage" styleClass="errorMessage" style="color:red;font-family: Verdana, Helvetica, Arial, Sans-serif;font-size: 8pt;font-weight: bold;"/>
       </rich:panel>
      
       </h:form>
       <rich:modalPanel id="panel" resizeable="false">
       <f:facet name="header">Edit Day:</f:facet>
       <f:facet name="controls">
       <h:panelGroup>
       <h:graphicImage value="/images/modal/close.png" id="hidelink" styleClass="hidelink"/>
       <rich:componentControl for="panel" attachTo="hidelink" operation="hide" event="onclick"/>
       </h:panelGroup>
       </f:facet>
       <h:form>
       <h:panelGrid columns="2" id="editContent">
       <h:outputText value="Option:"/>
       <h:selectOneRadio id="breakpointcategory" value="#{calendarDataModel.holidayOption}" immediate="true" >
       <f:selectItems value="#{calendarDataModel.holidayCategories}"/>
       </h:selectOneRadio>
       <a4j:commandButton value="Store" action="#{calendarDataModel.storeDayDetails}" id="storebutton" oncomplete="#{rich:component('panel')}.hide()" reRender="businessCalendar"/>
       <button type="button" id="cancelbutton" onclick="#{rich:component('panel')}.hide()">Cancel</button>
       </h:panelGrid>
       </h:form>
       </rich:modalPanel>


      I want to restrict the user to selecting date only in current year.
      hence a valdator is associated with the Calendar which invokes the method validateDate() on the Calendar bean.

      public void validateDate(FacesContext cont, UIComponent ucom, Object ob) throws Exception
       {
       Date da = (Date)ob;
       Calendar c = Calendar.getInstance();
       c.setTime(da);
       int year = c.get(Calendar.YEAR);
       if (c.get(Calendar.YEAR) != 2009) {
       FacesMessage message = new FacesMessage("Cannot set a holiday for " + year);
       cont.addMessage("calendarform:errorMessage", message);
       throw new ValidatorException(message);
       }
       }


      I can see the following message in the jboss server.log.

      WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.
      sourceId=calendarform:businessCalendar[severity=(INFO 0), summary=(Cannot set a holiday for 2010), detail=(Cannot set a holiday for 2010)]
      sourceId=calendarform:errorMessage[severity=(INFO 0), summary=(Cannot set a holiday for 2010), detail=(Cannot set a holiday for 2010)]

      But the modal panel is still displayed. How do I avoid displaying the modal panel if validation fails