How to validate currency/float precision on a form
jfrankman Oct 8, 2007 6:52 PMOther than doing it manually, is it possible to validate a currency or float amount on a form to make sure that it conforms to a certain precision and scale? I was hoping I could use an annotation on the entity or a f:convertnumber on the form but have not had any success.
I am trying to validate an input field to make sure the input currency amount has a precision of 7 and a scale of 2. I have tried the following:
1. Used <f:convertNumber type="currency" currencySymbol="$"/>
inside my input text field. RESULT: The form produces an error "value must be a number" even if the number is valid.
2. Used <f:convertNumber maxFractionDigits="2" minFractionDigits="2"/>
in the inputtext field,. RESULT: Any fraction amount is used. No validation occurs.
3. Use @Digits on the entity bean. RESULT: cannot find annotation. I presume that the @Digits is not included in the hibernate validator that comes with seam 1.2.1 GA.
My entity class is:
@Entity
@Table(name = "FBCLIENTTRANSLINEITEMS")
@Name("clientTransmittalLineItem")
@Scope(ScopeType.SESSION)
public class ClientTransmittalLineItemVO {
private int lineitemid;
private ClientTransmittalVO clientTransmittalVO;
private Double cashamount;
public ClientTransmittalLineItemVO() {
}
@Id
@Column(name = "LINEITEMID", unique = true, nullable = false)
@NotNull @GeneratedValue
public int getLineitemid() {
return this.lineitemid;
}
@Column(name = "CASHAMOUNT", precision=7, scale=2)
public Double getCashamount() {
return this.cashamount;
}
public void setCashamount(Double cashamount) {
this.cashamount = cashamount;
}
}My xhtml is:
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
xmlns:s="http://jboss.com/products/seam/taglib"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:a="https://ajax4jsf.dev.java.net/ajax"
xmlns:rich="http://richfaces.ajax4jsf.org/rich"
template="layout/frametemplate.xhtml">
<ui:define name="body">
<h:messages globalOnly="true" styleClass="message" id="globalMessages"/>
<h:form id="clienttranslineitems" styleClass="edit">
<rich:panel>
<f:facet name="header">Edit Transmittal Line Item</f:facet>
<s:decorate id="lineitemidDecoration" template="layout/edit.xhtml">
<ui:define name="label">Line Item ID</ui:define>
<h:inputText id="lineitemid"
required="true"
value="#{lineItem.lineitemid}">
<a:support event="onblur" reRender="lineitemidDecoration"/>
</h:inputText>
</s:decorate>
<s:decorate id="cashamountDecoration" template="layout/edit.xhtml">
<ui:define name="label">Cash Amount</ui:define>
<h:inputText id="cashamount"
value="#{lineItem.cashamount}">
<a:support event="onblur" reRender="checkamountDecoration"/>
<f:convertNumber type="currency" currencySymbol="$"/>
</h:inputText>
</s:decorate>
<div style="clear:both">
<span class="required">*</span>
required fields
</div>
</rich:panel>
<div class="actionButtons">
<h:commandButton
action="#{clientTransmittalLineItemAction.saveClientTransmittalLineItem}"
id="create" value="Save">
</h:commandButton>
</div>
</h:form>
</ui:define>
</ui:composition>My Environment:
JBoss 4.0.5
Seam 1.2.1 GA
Project is generated by seam-gen.