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

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?

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}" />

Related

How to round values in JSF?

Please help me to round values (example: from 4.23 to 4 and 4.5 to 5). I have code such as below. I need do this on my frontend part:
<p:column
styleClass="#{SomeClass.getAnotherSomething(row.magicCategory)}">
<h:outputLabel value="#{empty row.papaja ? 'N/A' : row.papaja}"
styleClass="textChangesInCells"
pt:data-selenium="papaja" />
</p:column>
You need to use h:outputText instead of h:outputLabel. With h:outputText you can use f:convertNumber to format a numeric value. With format="0" it'll round the value. So:
<h:outputText value="#{row.papaja}">
<f:convertNumber pattern="0"/>
</h:outputText>
To handle 'N/A', you need to do a check using c:if, c:choose, or use rendered attributes.

How to use today's date for a placeholder?

I got a <p:calendar> and want to use today's date as placeholder.
My attempt is to use OmniFaces #{now} in combination with a:placeholder for this:
<p:calendar id="calendar" a:placeholder="#{now}" minHour="8" locale="de"
maxHour="20" value="#{adminSeminarAlleBearbeitung.seminar.someproperty}"
pattern="dd.MM.yyyy HH:mm" />
Where someproperty is a java.util.date.
This is working so far, but I want to format the date like dd.MM.yyyy HH:mm.
How can I achieve this?
Use of:formatDate() function in EL:
<p:calendar ... a:placeholder="#{of:formatDate(now, 'dd.MM.yyyy HH:mm')}" />
Or if you'd like to reuse calendar component's own pattern attribute:
<p:calendar ... a:placeholder="#{of:formatDate(now, component.pattern)}" pattern="dd.MM.yyyy HH:mm" />
See also:
What exactly is #{component} in EL?

JSF - Formattting <p:calendar> using SimpleDateFormat

I have a <p:calendar> code in XHTML
<p:calendar
value="#{Bean.targetdate}"
pattern="MM/dd/yyyy" size="10" navigator="true"
rendered="#{Bean.editCmdActionflg == true}"
onblur="fChkDate(this);">
<f:convertDateTime type="date" pattern="MM/dd/yyyy" />
My problem is when I provide a invalid date such as 12/31/6666 the <p:calendar> accept the date as it is and I did SOP in the backing bean and I got Mon Dec 31 00:00:00 IST 6666 for the date that I enter.
I should not allow the user to enter a invalid date like the one that I entered above.
And I need to make sure the user should provide a valid date How can I do that ?
You can use yearRange property to reslve your problem, like-
<p:calendar yearRange="c-125:c+50" value="#{Bean.targetdate}" pattern="MM/dd/yyyy" readonlyInput="true" size="10" navigator="true"
rendered="#{Bean.editCmdActionflg == true}"
onblur="fChkDate(this);"/>

regex date validation on p:calendar

my calendar has readOnlyInput="false" , hence the user can enter wrong dates such as 13/13/2013.
is there a way to regexValidate my date in case the user prefers to type the date instead of using the datePicker popup ?
<p:calendar id="birthDate" size="22" locale="#{view.locale}"
maxdate="#{userCreationBean.maxDate}" navigator="true"
yearRange="c-100" readOnlyInput="false"
value="#{userCreationBean.user.birthDate}"
mindate="01/01/1900" pattern="dd/MM/yyyy"
style="left: 194px !important;"
>
</p:calendar>
The <f:validateRegex> validator works on String input values only, not on Date input values and is therefore insuitable for the purpose you had in mind.
Rather use the <f:convertDateTime> converter.
<p:calendar ...>
<f:convertDateTime pattern="dd/MM/yyyy" />
</p:calendar>
It's by default non-lenient and will thus throw a converter exception when an invalid date is entered. You can if necessary customize the converter message by converterMessage attribute on the input component.

validating and displaying decimal numbers in JSF 2.0

I wonder how to validate an inputText text field and see if it matchs a decimal format.
And also in the rendering time how to format that text field with a specific format
I've done this :
<rich:column id="soldes_comptables">
<f:facet name="header">
<h:outputText value="Solde Comptable" />
</f:facet>
<h:inputText id="inputTextSC" value="#{file.soldes_comptables}"
label="Montant"
style="border-color:#F2F3F7;"
validatorMessage="The number must be decimal eg: 000.00"
>
<f:convertNumber pattern="#,###,##0.00"/>
<f:validateRegex pattern="^[0-9]+(\.[0-9]{1,2})?$"></f:validateRegex>
<rich:validator />
</h:inputText>
<rich:message for="inputTextSC"/>
</rich:column>
but it's not working as i want :(. please help me
You're mixing validation and conversion. The <f:validateRegex> applies only on String values, not on Number values, however, the <f:convertNumber> has already converted it from String to Number beforehand, so the <f:validateRegex> is rather useless to you. You should remove it and specify the message as converterMessage instead.
<h:inputText ... converterMessage="The number must be decimal eg: 000.00">
<f:convertNumber pattern="#,###,##0.00"/>
</h:inputText>
An alternative would be to create a custom converter extending NumberConverter and throw a ConverterException on improper input format based on some regex pattern matching.
See also:
validating decimals inputs in JSF
How validate number fields with validateRegex in a JSF-Page?

Resources