Announcement Announcement Module
Collapse
No announcement yet.
Newbie: How can you bind an optional numeric value Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • 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?

    Kev

  • #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.

    Comment


    • #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?

      Kev

      Comment


      • #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 http://www.springframework.org/docs/...sResolver.html 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!

        Martin

        Comment


        • #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...

          Comment


          • #6
            ...hmm...

            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.

            Kev

            Comment


            • #7
              JIRA response

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

              Kev

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

              Juergen

              >>>>>>

              Comment


              • #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.

                toh

                Comment


                • #9
                  perfect, thank you!

                  Comment

                  Working...
                  X