Announcement Announcement Module
Collapse
No announcement yet.
Number editor in simpleFormController Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Number editor in simpleFormController

    Hi,
    in my application there are several BigDecimal fields in the model.

    For example, in the database the "price" field has 10 integer and 2 decimal digits

    In the initBinder function I wrote this code:


    DecimalFormat df = (DecimalFormat) NumberFormat
    .getInstance();
    df.applyPattern("#,##0.00");
    df.setParseBigDecimal(true);

    CustomNumberEditor numEditor = new CustomNumberEditor(BigDecimal.class,
    df, true);
    binder.registerCustomEditor(BigDecimal.class, "price", numEditor);



    The code works well but:
    1) If the user write "1ABC" in the input field, Spring doesn't show any error but create a BigDecimal cotaining 1.
    In this case I need tho show an error because for me the user has entered an invalid number.

    2) If the user write "12345.678901" in the input field, Spring doesn't show any error but create a BigDecimal with six decimal digits (but in the database the field has only 2 decimal digits).
    In this case I need to show an error, because the database will throw an exception if I try to save this record.


    Amy idea?
    Do I need to write a number editor instead of using Spring CustomNumberEditor?

    Thanks,
    Davide
Working...
X