JSF <h:inputText> should accept just number - jsf

i 've been looking a lot about how to make my allows just number to be inserted inside , seems quite easy but it doesn't work in anyway.
I have this code :
<p:fragment id="thresoldPrice">
<h:inputText type="number"value="" disabled="#{!tradingFunnelCBean.tradingFunnelMBean.conditionalOrder}" styleClass="#{!tradingFunnelCBean.tradingFunnelMBean.conditionalOrder ? '' : 'num-col'} middle-space">
<f:convertNumber pattern="#.####" integerOnly="true" maxFractionsDigit="4"/>
<p:watermark for="thresoldPrice"value="#{localizationBean.getMessageValue('EP3.GIMB.TRADINGFUNNEL.PRIVATE.LABEL.WATERMARK', languagePreferenceBean.selectedPreference)}" />
</h:inputText>
</p:fragment>
but it keeps accepting String values. i would also like to make the value inserted in this format "#.####" but the pattern attribute of the h:inputText doesn't make anything.
Any suggestion?

Related

JSF outputText and PrimeExtensions inputNumber [duplicate]

I am using JSF 2 and RichFaces 3. Here in the picture shown below, numbers are being displayed as what they are in the database.
But I want to display them as 6749395.20 if fraction part is there and 5095138.00 if no fraction part is there.
As of now I have tried something like this.
<rich:column>
<f:facet name="header">
<h:outputText value="Total Amount"/>
</f:facet>
<h:outputText value="#{rr[2]}">
<f:convertNumber type="number" groupingUsed="true" minFractionDigits="2" pattern="#0.00"/>
</h:outputText>
</rich:column>
Actually I am showing all of them together, but I have tried with all of them as all possible combinations with type, groupingUsed, minFractionDigits and pattern.
Why does it not work? How is this caused and how can I solve it?
That can happen if the value is not a Number at all, for example a String. You're then basically using the wrong type for the data it represents. To represent currencies in Java, you should be using BigDecimal. Also, make sure that the type in the database table is right, i.e. it should not be a varchar, but a decimal.
Once you've fixed the data type, then the <f:convertNumber> will work as you told it to do. Note that the pattern attribute will override the groupingUsed and minFractionDigits. You should use either the pattern or the others. Also, type="number" is already the default, so it can be removed.
So, either use
<f:convertNumber pattern="#0.00" />
or
<f:convertNumber groupingUsed="true" minFractionDigits="2" />
Note that they generate different formats. You probably want to set grouping to false.
You can also use type="currency", it will then automatically apply the right pattern as per the UIViewRoot#getLocale():
<f:convertNumber type="currency" />
See also the tag library documentation and the DecimalFormat javadoc.

f:convertNumber does not format seperator correctly in ui:composition

in my application we're using some input fields directly, and some via a template. The strange thing is that on the inputfield in the template the separator is a dot (.) and those outside the template is a ,
Both inputtexts are completely equal, we even tried to set the same locale for both without success:
in this snippet it's a ,
<p:inputText value="#{manageContracts.dieselFloater}"
id="dieselFloater" required="true">
<f:convertNumber maxFractionDigits="2"
minFractionDigits="2" locale="de"/>
</p:inputText>
in this one it's a . (inside an ui:composition):
<p:inputText value="#{_price}" style="width:140px">
<f:convertNumber maxFractionDigits="2" minFractionDigits="2" locale="de" />
</p:inputText>
does anyone have an idea?
You can use pattern="" in <f:convertNumber> to specify wether you want a dot (.) or a coma (,).
See documentation : http://www.jsftoolbox.com/documentation/help/12-TagReference/core/f_convertNumber.html
See example : http://www.mkyong.com/jsf2/jsf-2-convertnumber-example/
EDIT :
To fix the decimal separator, see this workaround : JSF Locale Change Listener not persistent
It consists to wrap your <ui:composition> in a <f:view locale="de">

f:convertNumber silently converts invalid numbers like 1a to 1 without showing conversion error message

I try to check if an input text is a Integer or not. I have wrote this piece of code:
<h:inputText id="amount" value="#{receipt.amount}" required="true" converterMessage="error" requiredMessage="error" validatorMessage="error" >
<f:validateLongRange minimum="1" maximum="#{receipt.all}" />
<f:convertNumber IntegerOnly="true" />
</h:inputText>
and it is working but in one case if I put for example "1a" it converts me this to "1". How can I check this in JSF? I would like to see a message if input is incorrect. I have made validation in my bean and everything worked but I have to do in JSF. Could anybody explain how to set maximum="#{receipt.all} correct? If I put in this way I have a zero. I try to refres this input but it didn't work.

Format a JSF converted value

Today I noticed ocpsoft has a nice time library adapted to use in JSF 2 as a converter. The strong point of that is you can use the converter directly in the date displayed in the view and it converts it into a string telling the user something like 6 hours ago or 17 hours from now. I think my best is to combine both, the JSF converted date and this one to display something like 26-03-2013 17:00 (4 hours from now). I can achieve something similar with the following code:
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>
My problem comes when I want to put the second value into parenthesis. The PrettyTimeConverter accepts only a date as a value and I can't write the parenthesis there directly. Also JSF is not accepting the following:
<h:outputFormat value="({0})">
<f:param value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</f:param>
</h:outputFormat>
With that I have the following error:
<f:converter> Parent not an instance of ValueHolder:
javax.faces.component.UIParameter#1492636
Any idea about how to achieve that avoiding writing both parentheses using specific h:outputText tags?
You can just put those parentheses directly in template text without the need for another <h:outputText>s.
<h:outputText value="#{task._StartDate}" style="padding:2px;">
<f:convertDateTime pattern="dd-MM-yyyy HH:mm" timeZone="GMT+1" />
</h:outputText>
(<h:outputText value="#{task._StartDate}">
<f:converter converterId="org.ocpsoft.PrettyTimeConverter" />
</h:outputText>)
See also:
Is it suggested to use h:outputText for everything?

Formatting a double in JSF

I have a problem similar to the one found here : JSF selectItem label formatting.
What I want to do is to accept a double as a value for my and display it with two decimals. Can this be done in an easy way?
I've tried using but that seems to be applied on the value from the inputText that is sent to the server and not on the initial value in the input field.
My code so far:
<h:inputText id="december" value="#{budgetMB.december}" onchange="setDirty()" styleClass="StandardBlack">
<f:convertNumber maxFractionDigits="2" groupingUsed="false" />
</h:inputText>
EDIT: The above code actually works. I was fooled by JDeveloper that didn't update the jsp page even when I did a explicit rebuild of my project and restarted the embedded OC4J server. However, after a reboot of my computer everything was fine.
If I'm not misunderstanding your requirement, I was able to achieve formatting of the value in the input box during the rendering of the view with:
<h:inputText id="text1" value="#{...}">
<f:convertNumber pattern="#,###,##0.00"/>
</h:inputText>
I was using the Standard Faces Components in my vendor-branded Eclipse so I'm assuming the pattern attribute is part of standard JSF.
If what you are trying to do is make the value of the input text field change on screen (to correct user input), you should probably look into using one of the JSF ajax frameworks like Rich Faces.
A possible example would look like this:
<h:inputText id="december" value="#{budgetMB.december}" styleClass="StandardBlack">
<f:convertNumber maxFractionDigits="2" groupingUsed="false" />
<a4j:support event="onblur" reRender="december" />
</h:inputText>
I haven't tested this, but I think it may work.
It seems you're actually formatting a currency. There already exists a specific formatter to handle currencies that you can assign many options to:
<f:convertNumber type="currency" />
Some interesting attributes of this tag are: locale, currencyCode, integerOnly, currencySymbol and pattern.

Resources