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

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

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.

JSF <h:inputText> should accept just number

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?

Custom data types in Facelets JSF 2 Expression Language

How to display a custom property using facelet expression language?
For example:
<h:outputText value="#{contact.customTypeProperty}" />
where customTypeProperty is of type CustomClass, and I want to display the String returned by its toString()?
That should already be the default behaviour. You don't need to change anything on the given code example, assuming that the toString() method is properly implemented on the CustomClass. However, if it returns HTML, you'd need to add escape="false" to the output text to prevent JSF from auto-escaping it (which it does in order to prevent XSS attacks on user-controlled input):
<h:outputText value="#{contact.customTypeProperty}" escape="false" />
This is however not necessarily the best practice. You should control the presentation in the view side, not in a toString() in the model side. For example, assuming that CustomClass has in turn two properties foo and bar and you'd like to present it in a table:
<h:panelGrid columns="2">
<h:outputText value="Foo" />
<h:outputText value="#{contact.customTypeProperty.foo}" />
<h:outputText value="Bar" />
<h:outputText value="#{contact.customTypeProperty.bar}" />
</h:panelGrid>
If you did this to avoid code repetition, then you should actually be using an include file or a tag file. See also When to use <ui:include>, tag files, composite components and/or custom components?

Can we apply mask operation with <p:calendar>

<h:column>
<f:facet name="header">
<h:outputLabel value="#{label.asOfDate}" style="font-weight:bold" />
<h:outputLabel value="*"
style="font-weight:bold; color:red; font-size:150%" />
</f:facet>
<p:calendar id="date" required="true" navigator="true"
mindate="#{utils.minDate}" pattern="#{label.dateFormat}"
maxdate="#{utils.maxDate}" value="#{policy.asOfDt}"
requiredMessage="#{label.asOfDateRequired}" showOn="button">
<f:validator validatorId="CustomDateValidator" />
</p:calendar>
</h:column>
I want date textfield should in masked format (11/11/2011) with calendar button. Can we apply mask operation with calendar?
You can use somehting like this:
`
<script type="text/javascript" language="JavaScript">
var $ = jQuery;
$(document).ready(function() {
$("input[id*='Date']").mask('99/99/9999');
});
</script>
<p:calendar id="documentDate" />
`
Since Primefaces 5.0, you can use the calendar's attribute mask:
mask="99/99/9999"
apply datePattern attribute
datePattern="MM/dd/yyyy"
As to this post, it is not possible. The question there is answered by the PF lead developer (so trust him ;-). He suggests to write a composite component in combination with jquery (http://digitalbush.com/projects/masked-input-plugin/).
INMO , If you must use additional jquery plugin to achieve this task , you better use jquery for both : calendar and mask (a really little additional code is needed to bind bean value to the input responsible for the calendar)
take a look at this solutions
Mask the date format when using jQuery UI datepicker
and
Small jsfiddle example
and another one
Using jQueryUI Datepicker And Input Masking Together
You might address this problem by setting the input field to readonly to force users to select a date.
Here is the property to add to your input field to force this :
readonlyInput="true"

Check for validation errors in part of a form in JSF

I'm building a Seam application, which is basically a huge form divided into different parts, or modules. I need a way to figure out when a module is "complete", meaning all validation for the fields in that module passes. I then need to do something in the view, setting a css-class or whatever.
Something like:
<a:region id="region1">
<s:div styleClass="#{invalid ? 'errors' : ''}">
<h:inputText required="true" id="input1" />
<h:inputText required="true" id="input2" />
<h:commandButton value="Save this section" reRender="region1" />
</s:div>
</a:region>
I figured I had two options:
Using some sort of view-logic (like #{invalid} for a single field)
Using a method in the bean, where I get all components for the module programmatically, and check them for validation errors.
However, I can't find any way to do any of them. Any ideas if this is even possible?
We're using JSF 1.2 with Seam.
Thanks.
You can use UIInput#isValid() to check if a validation error has occurred on the particular input component.
<s:div styleClass="#{!input1.valid or !input2.valid ? 'errors' : ''}">
<h:inputText binding="#{input1}" required="true" id="input1" />
<h:inputText binding="#{input2}" required="true" id="input2" />
<h:commandButton value="Save this section" reRender="region1" />
</s:div>

Resources