4 Replies Latest reply on Oct 9, 2007 6:35 PM by matt.drees

    How to validate currency/float precision on a form

      Other 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.

        • 1. Re: How to validate currency/float precision on a form
          matt.drees

          If I were you, I'd try to get annotation-based validation working.

          So, either:
          -try including a newer Hibernate Validator jar
          -write your own (i.e. copy/paste Hibernate's)

          • 2. Re: How to validate currency/float precision on a form

             

            "matt.drees" wrote:
            If I were you, I'd try to get annotation-based validation working.

            So, either:
            -try including a newer Hibernate Validator jar
            -write your own (i.e. copy/paste Hibernate's)


            Matt, thanks for your help.

            Do you happen to know what version of the hibernate-validator jar to use? The jboss downloads page only offers 3.0. My project was built using the seam-gen tool. So it is using the hibernate-all.jar. Do I need to replace this with all new hibernate jars or can I simply add the new hibernate-validator.jar?

            • 3. Re: How to validate currency/float precision on a form

              In case someone else needs to do this:

              At first I downloaded the hibernate-validator.jar and tried to add it to my project and jboss, but I could not get it to work with the correct version of the hibernate-entity manager. So, I downloaded the hibernate entity manager 3.3.1 which included the latest hibernate validator and all supporting jar files. This worked much better. Deploy the hibernate entity manager jar and all the jars in the lib directory as one unit. It is much easier than downloading the validator and trying to find all the other jars that need updated to work with it. I moved the jar files from the entity manager 3.3.1 download and added them to jboss and to my project. I also had to download the core hibernate 3.2 and add the hibernate3.jar.

              Once I did this I was able to get the scale and precision to work as follows:

              @Column(name = "CHECKAMOUNT", precision=7, scale=2)
               @Digits(integerDigits=7,fractionalDigits=2)
               public Double getCheckamount() {
               return this.checkamount;
               }




              • 4. Re: How to validate currency/float precision on a form
                matt.drees

                Glad to hear it worked out. :-)