Announcement Announcement Module
Collapse
No announcement yet.
Can you specify the maximum number of decimal places for a field? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Can you specify the maximum number of decimal places for a field?

    Is it possible to specify the maximum number of decimal places allowed in a given field, using either:
    • a field-level option in the roo shell, or
    • a JSR-303 annotation on that field?
    I have a java.math.BigDecimal field that contains a currency amount, and I want to validate that the amount doesn't contain more than say 3 decimal places (the maximum number of decimal places in any currency, AFAIK). Also I want the maximum number of decimal places to determine the scale of the relevant database column (e.g. NUMBER(10,3)).

    I've found the @DecimalMax annotation, but from reading the JavaDoc, it seems to limit the value of the entire field.

    If there's no way of doing this, is it possible to write your own annotations and have them applied by the JSR-303 provider?
    Last edited by Andrew Swan; Jan 9th, 2010, 07:32 PM. Reason: added requirement about scale of database column

  • #2
    You can define your own annotations which would work in conjunction with JSR-303 Bean Validation. If you take a look at the specification you can find a few examples which describe the process.

    Alternatively you could use the @javax.validation.constraints.Pattern annotation which allows you to specify the exact pattern for your decimal number via a regular expression.

    HTH

    Comment


    • #3
      I've recently used @Column, that is a javax.persistence annotation, and it seems its working fine.

      You can see it at:
      http://java.sun.com/javaee/5/docs/ap...ce/Column.html

      @Column(precision=12, scale=2)

      Comment


      • #4
        For those not bound to annotations you could use Forms4j (www.forms4j.com), which has an in-built validator for currency.

        Code:
        <forms4j:input id="foo">
        	<forms4j:validator pattern="MONEY" />
        </forms4j:input>

        Comment

        Working...
        X