title: Two primefaces calendar component validation
body: <p>I've a form in <strong>JSF 2</strong> where I'm using a double field to specify a range of dates. The aim of that is not to let the user to pick a first date which is before the second one. So I want to perform a validation before the form is sent, using <code>p:calendar</code> components. What I do is to tie a validator to the second calendar input in order to access the first component internally and compare dates.</p>
  <p>Here it is my <em>xhtml</em> page:</p>
  <pre class="lang-xml prettyprint-override"><code>&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;
      xmlns:h=&quot;http://java.sun.com/jsf/html&quot;
      xmlns:f=&quot;http://java.sun.com/jsf/core&quot;
      xmlns:ui=&quot;http://java.sun.com/jsf/facelets&quot;
      xmlns:p=&quot;http://primefaces.org/ui&quot;&gt;
  
  &lt;h:head /&gt;
  &lt;h:body&gt;
      &lt;h:form id=&quot;date_form&quot;&gt;
          &lt;p:calendar id=&quot;date1input&quot; value=&quot;#{dateTestBean.date1}&quot; /&gt;
          &lt;p:calendar value=&quot;#{dateTestBean.date2}&quot; validator=&quot;dateValidator&quot;&gt;
              &lt;f:attribute name=&quot;date1&quot; value=&quot;#{date1input}&quot; /&gt;
          &lt;/p:calendar&gt;
          &lt;p:commandButton value=&quot;Submit&quot; action=&quot;#{dateTestBean.submit}&quot;
              ajax=&quot;false&quot; /&gt;
      &lt;/h:form&gt;
  &lt;/h:body&gt;
  &lt;/html&gt;
  </code></pre>
  <p>My managed bean:</p>
  <pre class="lang-java prettyprint-override"><code>@ManagedBean
  @ViewScoped
  public class DateTestBean {
  
      private Date date1;
  
      private Date date2;
  
      public Date getDate1() {
          return date1;
      }
  
      public void setDate1(Date date1) {
          this.date1 = date1;
      }
  
      public Date getDate2() {
          return date2;
      }
  
      public void setDate2(Date date2) {
          this.date2 = date2;
      }
  
      public void submit() {
          System.out.println(&quot;Submited &quot; + date1 + &quot; &quot; + date2);
      }
  
  }
  </code></pre>
  <p>My validator class:</p>
  <pre class="lang-java prettyprint-override"><code>@FacesValidator(value = &quot;dateValidator&quot;)
  public class DateValidator implements Validator {
  
      @Override
      public void validate(FacesContext context, UIComponent component,
              Object value) throws ValidatorException {
          System.out.println(((UIInput) context.getViewRoot().findComponent(
                  &quot;date_form:date1input&quot;)).getValue());
          UIInput date = (UIInput) component.getAttributes().get(&quot;date1&quot;);
          System.out.println(date);
          //Will perform date comparison
      }
  }
  </code></pre>
  <p>The output I get here sending <code>2013-10-10</code> as first date and <code>2013-10-12</code> as second one is:</p>
  <blockquote>
  <p>Thu Oct 10 00:00:00 CEST 2013</p>
  <p>null</p>
  <p>Submited Thu Oct 10 00:00:00 CEST 2013 Sat Oct 12 00:00:00 CEST 2013</p>
  </blockquote>
  <p>Which suggests <code>f:attribute</code> tag is not working for <code>p:calendar</code> component <a href="http://balusc.blogspot.com.es/2007/12/validator-for-multiple-fields.html" rel="nofollow noreferrer">as it does for other input components</a>. However, I can access the first calendar via view root, but enforces me to know the entire path of the client id of the component I want to validate. Both values are set in the managed bean later with no issues.</p>
  <p>A workaround would be <a href="http://qussay.com/2013/07/10/date-range-validation-with-primefaces-calendar/" rel="nofollow noreferrer">to use an ajax call when first date changes</a> in order to place it in the model and send it as date itself in the <code>f:attribute</code> instead of sending the UIInput (I'm already doing it that way).</p>
  <p>Isn't there a better way? Perhaps I have to join them in a single component like in <a href="https://stackoverflow.com/questions/19275847/validation-conversion-for-two-component-based-composite-in-jsf">this case</a>?</p>