I have this piece of code:
<p:inputText value="#{addnewfields.price}">
<f:convertNumber type="number" />
</p:inputText>
So what I want to do is to display my number with space separators.
But still in my input it looks like 1000000 instead of 1 000 000.
Any ideas?
A possible solution you can try is to create your own converter. The InputText value is a String, you have to manage the conversion from this String to the object number you want.
The converter has two functions to implement: from the object (binded value) to String (text shown in the input)
and from the String to the object.
You can see a converter example here: http://www.mkyong.com/jsf2/custom-converter-in-jsf-2-0/
Related
I used this regex ^(\+968|968|00968)?\d{8}$ for a phone number. It must be 8 digits and start with 9 or 7.
<p:inputText value="#{customer.phoneNo}" maxlength="8" required="true"
validatorMessage="Phone Number Should be 8 digits and start with 9,7">
<f:validateRegex pattern="^(\+968|968|00968)?\d{8}$"/>
<f:validateLength minimum="8" maximum="8"/>
<p:keyFilter mask="num"/>
</p:inputText>
but I'm getting this error:
java.lang.Integer cannot be cast to java.lang.String
In the bean phone number is an Integer.
f:validateRegex expects a String value (or CharSequence depending on the JSF implementation), so it cannot be used on an Integer. So you need to use a String value in your UI and convert that to numeric before saving it to the database. You could use an AttributeConverter for that if you are using JPA. But I must say that storing a phone number as a numeric database field is a strange choice in my opinion.
See also:
https://github.com/eclipse-ee4j/mojarra/blob/master/impl/src/main/java/jakarta/faces/validator/RegexValidator.java
https://github.com/apache/myfaces/blob/master/api/src/main/java/jakarta/faces/validator/RegexValidator.java
It means java.lang.Integer cannot be java.lang.String you need to convert it.
I'm implementing a dialog for registering an address. In the street name field, I'm using PrimeFaces' <p:autoComplete> tag to help the user find the name of their street. The completeMethod works as expected, but to avoid confusion between similar street names I would like the drop-down list to also include the municipality the given street is in, for instance on the format "<street name>, <municipality name>".
I don't want the municipality name to be included in the actual field, so I've concluded that I need a method that performs some sort of string manipulation (substring using the position of the first comma, for instance), but I can't figure out where such a method would be called from. I've had a look through the PrimeFaces documentation, but I haven't been able to find anything that would allow me to do this. Is this at all possible in <p:autoComplete>? Alternatively, is there another autocomplete implementation which supports this, or would I have to implement my own javascript component?
EDIT: This is what the xhtml code I'm using looks like:
<div class="form-group row required">
<h:outputLabel value="#{msgs['#common.mailingAddress']}" for="address" styleClass="col-xs-12"/>
<p:autoComplete
id="address"
name="address"
size="50"
maxlength="50"
styleClass="col-xs-12 street-name"
label="#{msgs['#common.search']}"
disabled="#{not configurationController.cardCtrl.editable}"
value="#{configurationController.cardCtrl.selected.address}"
required="true"
completeMethod="#{configurationController.cardCtrl.autoCompleteTest}">
<f:validator binding="#{onlyLettersOrDigitsValidator}"/>
</p:autoComplete>
<h:message id="addressMessage" for="address" styleClass="inline-error inline-error-small"/>
</div>
The autoCompleteTest method in the controller is as follows:
public List autoCompleteTest(String input) {
AddressSearch addressSearch = AddressSearch.builder()
.streetName(input)
.municipality(municipality.getName())
.maxResultsPerPage(10)
.build();
return addressesToStreetNames(mapService.addressSearch(addressSearch).getAddresses());
}
With a helper method addressesToStreetNames which takes a list of Address objects and returns an ArrayList<String> containing those addresses' street names.
EDIT2: Based on suggestions in the comments, I tried setting itemValue and itemLabel to different values, to see if that had any effect. The new xhtml looks like the above, with the addition of the following three lines:
var="address"
itemValue="#{address.streetName}"
itemLabel="#{configurationController.cardCtrl.formatAddress(address.streetName, address.postTown)}"
The autoCompleteTest method now also returns the Address object directly rather than a String representation of the street name, so that these fields are available. The formatAddress method is simply return streetName + ', ' + postTown;
This causes the dropdown list to look how I want it to look, but when I click an item it still inserts the whole string with both street name and post town/municipality into the text field (and in fact, before I've written anything, the text field already contains ", ").
i want a comma decimal separator when user press decimal key, but after many test, not work any.
number is String type why on db is definited string
tried
<p:inputText value="#{testController.number}">
<f:convertNumber locale="it"/>
</p:inputText>
tried
<default-locale>it_IT</default-locale>
in faces-config
but any success.
Suggest?
If you use <p:inputNumber> it automatically generates commas for your integers.
I want to hide the minus value from the print during formatting in JSF. How can I achieve that.
For Example,
The Value : -340
Need to Display as : 340.00
Not like -340.00 or (340.00)
Is that possible? The current code is like that, but can not hide minus value.
<h:outputText value="#{paymentBill.amount}" >
<f:convertNumber pattern="#0.00" />
</h:outputText>
If it's <h:outputText> then you can use arbitrary EL expression as its value, if you're so dissatisfied with getting absolute value while preparing data in your managed bean, as it's rightly proposed by perissf and fareed, like:
value="#{(paymentBill.amount lt 0) ? -paymentBill.amount : paymentBill.amount}"
You can return the absolute value of the amount In the backing bean.
Return Math.abs(amount) instead of amount.
Is there any inbuilt number validator tag in JSF that checks whether an input entered in h:inputext field is a number?
The first question was answered. Edited to explain the next problem:
<h:inputText id="maxrecs" value="#{simpleBean.numRecords}" required="false" maxlength="4" >
<f:convertNumber longOnly="true"/>
</h:inputText>
Backing Bean
private long numRecords = null;
If I use String or Integer object in the backing bean , value is not being set. Now when I use primitive int, 0 is being printed on the screen. I would like the screen to be empty.
You can use f:convertNumber (use the integerOnly attribute).
You can get more information here.
You can use:
<f:validateLongRange minimum="20" maximum="1000" />
Where minimum is the smallest number allowed and maximum is the largest.
Look here for more details
JSF Number validation for inputtext
mention f:converterNumber component in between h inputText component and mention the attributes integerOnly and type.
<h:inputText id="textMobileId" label="Mobile" styleClass="controlfont" value="#{UserRegistrationBean.textMobile}">
<f:convertNumber integerOnly="true" type="number" />
</h:inputText>
If you enter abcd in Mobile textbox at the time when you click on commandbutton it automatically shows an error like
Mobile: 'abcd' is not a number.
i8taken solution converts number into long without validation message (at least in my case: JSF2 / global messages per page). For proper validation message you can
1. check value in action method in bean;
or
2. use converter attribute for inputText:
<h:inputText id="maxrecs" value="#{simpleBean.numRecords}" maxlength="4" converter="javax.faces.Integer" />
You can simply use the passthrough, so first add this library
xmlns:pt="http://xmlns.jcp.org/jsf/passthrough"
and after use this
<h:inputText id="numberId" pt:type="number" />