validating and displaying decimal numbers in JSF 2.0 - jsf

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?

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 reset <f:ajax> disabled attribute

given the following code:
<h:inputText id="minRate" value="#{bakingController.minRate}">
<f:convertNumber type="currency" pattern="#{msg.currencyPattern}" maxFractionDigits="0"/>
<f:ajax event="keyup" listener="#{bankingController.listProviders()}" execute="minRate" render="minRate btn-submit" disabled="#{facesContext.validationFailed}"/>
</h:inputText>
i would like to disable the ajax-listener if the validation is failed for the first keyup-event, e.g. user enters some non-digits. but when i clear the input field or enter a valid number then, it won't get reactivated, although the parent field is re-rendered. how can i solve that?
may you answers help.
there was a thinking flaw -.-
i have to validate with javascript if the input matches the appropriate format. then the listener will only be called, if the javascript-function returns true. so the disabled attribute is not needed.
<h:inputText id="minRate" value="#{bakingController.minRate}" onkeyup="return $.isNumeric(this.value)">
<f:convertNumber type="currency" pattern="#{msg.currencyPattern}" maxFractionDigits="0"/>
<f:ajax event="keyup" listener="#{bankingController.listProviders()}"
execute="minRate" render="minRate btn-submit"/>
</h:inputText>

How to hide validatorMessage when field is empty

Please help how to hide validatorMessage when user clearing the field:
<div class="item">
<p:outputLabel for="firstName" value="#{msgs['customerForm.firstName']}"/>
<p:inputText id="firstName" value="#{customerBean.customer.firstName}"
requiredMessage="#{msgs['Error.firstName.mandatory']}"
validatorMessage="#{msgs['Error.firstName.wrongFormat']}"required="true">
<f:validateRegex pattern="^([a-zA-Z]+[a-zA-Z\s\-]*){1,255}$" />
</p:inputText>
<p:message id="m_firstName" for="firstName" display="text"/>
</div>
You defined your field having two validations:
* required value validator
* regular expression validator
Both validators have equivalent priority, and both are run against your field value. And both of these validators see empty value as a problem, and your framework, unable to determine which of these two equivalent-looking failures is the "actual" failure, displays both.
To "fix" it, you should allow empty values to pass your regex, like this:
<f:validateRegex pattern="^([a-zA-Z]+[a-zA-Z\s\-]*){0,255}$" />
Notice that I changed number qualifier to allow 0-255 characters instead of 1-255 characters like before.
That should allow two of your validators cover different cases of invalid values, like you intended.
You can update the error messages if you put this in the input element. It will validate the input on every keyup event though.
<f:ajax execute="#this" event="keyup" render="m_firstName" />

JSF number converter issue

I am using jsf number converter in my application as follows:
<p:inputText id="hoursWorked" required="true"
value="#myBean.hours">
<f:convertNumber locale="de" maxFractionDigits="2"/>
</p:inputText>
Now , if I enter a value such as 160ffff, the converter automatically strips off the letters after the digits whereas I would like to throw an error. How can I do it?

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.

Resources