Search code examples
jsfprimefacesomnifaces

Validate p:calendar value to be not greater than today's date


I am using the component to allow the user pick the date they were born (Birth Date). I should display an error message if the user picks a day greater than today's date to prevent the age for been a negative number. My component looks like this:

<p:calendar id="fechaNacimiento" yearRange="c-100:c"
    pattern="dd/MM/yyyy" navigator="true"
    value="#{afiliadoController.afiliado.fecha_nacimiento}"
    requiredMessage="Debe de insertar la fecha de nacimiento del Afiliado."
    showOn="button"
    readonly="#{facesContext.currentPhaseId.ordinal eq 6}"
    required="#{request.getParameter('validate')}">

</p:calendar>

I am using PrimeFaces and OmniFaces and JSF 2.2. I was trying to validate this by using <o:validateOrder components=""/> but I would need two component and I only have one. What other options do I have?


Solution

  • PrimeFaces <p:calendar> has a maxdate attribute representing maximum selectable Date.

    OmniFaces has a #{now} variable representing current Date.

    Do the math.

    <p:calendar ... maxdate="#{now}" />