Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Yes, you can! Suppose you have the following PrimeFaces's input fields:</p> <pre><code>&lt;p:calendar id="from" value="#{mrBean.fromDate}" binding="#{from}" &gt; &lt;p:ajax process="from to" update="toDateMsg" /&gt; &lt;/p:calendar&gt; &lt;p:calendar id="to" value="#{mrBean.toDate}" &gt; &lt;f:attribute name="fromDate" value="#{from.value}" /&gt; &lt;f:validator validatorId="validator.dateRangeValidator" /&gt; &lt;p:ajax process="from to" update="toDateMsg" /&gt; &lt;/p:calendar&gt; &lt;p:message for="to" id="toDateMsg" /&gt; </code></pre> <p>This should be your <code>Validator</code>:</p> <pre><code>@FacesValidator("validator.dateRangeValidator") public class DateRangeValidator implements Validator { @Override public void validate(FacesContext context, UIComponent component, Object value) throws ValidatorException { if (value == null || component.getAttributes().get("fromDate") == null) return; Date toDate = (Date) value; Date fromDate = (Date) component.getAttributes().get("fromDate"); if (toDate.after(fromDate)) { FacesMessage message = new FacesMessage("Invalid dates submitted."); message.setSeverity(FacesMessage.SEVERITY_ERROR); throw new ValidatorException(message); } } } </code></pre> <p>Note that I am using PrimeFaces's <code>&lt;p:calendar&gt;</code> component to write my example because the properties binded to this component will automatically be converted to <code>Date</code> object before being validated. In your program, you may have your own <code>Converter</code> to convert <code>String</code> to <code>Date</code>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload