Announcement Announcement Module
No announcement yet.
Newbie: How can you bind an optional numeric value Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Newbie: How can you bind an optional numeric value

    I've got a form which consists of

    id: Long
    name: String

    Now in some circumstances id will be null, however when Spring binds 'id' it throws an error. Is there an easy way to make Spring bind without complaining?


  • #2
    You should be able to customize the Bind; I can't recall the exact class/interface names, but if you look through the docs you should be able to figure it out pretty quick.


    • #3
      I'm aware of the ability to create custom bindings ...

      ... but to my thinking this is not the correct default behaviour. :?

      If you have a 'long' in your form/command then fair enough, binding should insist that there is a value, but if you have a 'Long' then Spring should not insist that the value is set. It should bind empty form entries to null.

      Anyone else?



      • #4
        I have the same problem but with a required Integer field. I'm using validation in front of my command object, where my Integer property depends=required,integer. The commons validation engine identifies that the value was missing, but on the postback to the form it reports that the empty string cannot be parsed, typeMismatch. I found this helpful in customising the nasty message. But it's missing the point somewhat - if it's optional or I'm catching the validation error, then don't bind the missing/invalid value if it's going to throw an exception!



        • #5
          I have a similar(?) problem. One of the fields in my backing bean is "quantity" of type int. If I type not a valid int value into the form field, the validator throws the NumberFormatException and shows the exception message as a normal failed validation message (in the <spring:bind><spring:hasBindErrors>...-tags) - is it possible to catch this exception anyhow and to show my own error message instead? The problem is, where I can catch this exception. I assume in the form controller class, but I have no idea where. I have tried to do this in the formBackingObject-method, but it didn't succeed...


          • #6

            I think the issue of changing the binding errors is a related, but seperate issue to the one Martin and I are having.

            Our problem is that binding to a Long or Integer object complains when the value is null. Which must be wrong! I'm gonna try and log this as a bug.

            If I do come across anything regarding overriding binding error messages I'll post back here.



            • #7
              JIRA response

              I posted the problem as a bug and I got the following response - which seems fair enough.


              I suppose you want to bind an empty String parameter to a Long field, > resulting in a null value for the Long. This is indeed not possible by default: because of the JDK's default JavaBeans bindings, not because of Spring. (Remember that the core JavaBeans infrastructure is provided by the JDK.)

              What you can do in Spring is to customize the binding process. In this particular case, you could register a CustomNumberEditor with allowEmpty=true. Assuming you're in a command or form controller, you could override the initBinder method accordingly:

              protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
              binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true)); }

              The same would work on any DataBinder or BeanWrapper, even when used in a custom fashion.




              • #8
                You just need to register a custom number editor in the initBinding method for the form.

                protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) {
                binder.registerCustomEditor(Integer.class, null, new CustomNumberEditor(Integer.class, true));
                binder.registerCustomEditor(Double.class, null, new CustomNumberEditor(Double.class, true));

                This will set all Integer and Double fields to allow null.



                • #9
                  perfect, thank you!