3 Replies Latest reply on Jul 22, 2009 2:10 PM by jmaglio

    get value of rich:calendar for validation

    jmaglio

      Hi
      I am trying to get the date value from a <rich:calendar> to validate that it is before another date, but it is null. Here is the general idea, please let me know what is wrong:

      <h:form id="searchForm">

      <rich:calendar id="fromDate" />

      <rich:calendar id="toDate"
      validator="#{bean.checkDates}">
      <f:attribute name="fromDate" value="searchForm:fromDate" />
      </rich:calendar>

      </h:form>

      public void checkDates(FacesContext context, UIComponent component, Object value) {

      String fromDate = (String) component.getAttributes().get("fromDate");
      // This 'date' is null:
      HtmlCalendar date = (HtmlCalendar) context.getViewRoot().findComponent(fromDate);
      Date startDate = (Date) date.getData();

      if( !(startDate.before(endDate) ){
      ...
      }
      }

      Thanks in advance
      J

        • 1. Re: get value of rich:calendar for validation
          alexsmirnov

          That is common JSF issue - multi-field navigation is very hard to implement.
          At the PROCESS_VALIDATORS phase 'value' attribute not yet assigned, therefore you get invalid date for a 'startDate' component.
          You should use value argument from validate method call for the both dates. Suggested implementation is:

          <rich:calendar id="fromDate" validator="#{bean.checkFromDate}" />
          
          <rich:calendar id="toDate" validator="#{bean.checkToDate}">
          </rich:calendar>
          

          The bean should be request scope and 'checkFromDate' does nothing but store submitted date into a some field.
          in the 'checkToDate' method you can compare both values and throw validation exception for incorrect order.

          • 2. Re: get value of rich:calendar for validation
            nbelaevski

            I guess the problem is

            Date startDate = (Date) date.getData();


            • 3. Re: get value of rich:calendar for validation
              jmaglio

              Thanks, Alex. That worked!