Results 1 to 6 of 6

Thread: spring:bind error when converting empty input field to Long

  1. #1

    Default spring:bind error when converting empty input field to Long

    I am using a SimpleFormController with Spring:bind tag on my jsp and a validator object.

    I have a simple Long property which maps to a simple input field, it validates numbers ok but if no value is entered at all it throws a conversion error.

    Please how do I handle this simple case, surely I dont have to write a property editor for this.

  2. #2
    Join Date
    May 2005
    Location
    California, US
    Posts
    735

    Default

    Where is it throwing? In the validator or in the view?

    Can you show us the code where it's throwing?

  3. #3
    Join Date
    Aug 2004
    Location
    Atlanta, GA
    Posts
    129

    Default

    By default, the CustomNumberEditor doesn't accept empty/null input. Put the following line in your controller initbinder() method:
    Code:
    binder.registerCustomEditor(Long.class, new CustomNumberEditor(Long.class, true));
    Randy

  4. #4

    Default

    I tried that and was pretty confident it would solve the prpblem but it hasnt actually made any difference, the exact error I get is:
    Failed to convert property value of type [java.lang.String] to required type [java.lang.Long] for property 'volume'; nested exception is java.lang.NumberFormatException: For input string: " "

  5. #5

    Default

    Sorry I had only done a deploy not a real build, it has made a difference but code now falls over when I call ValidationUtils in my validator to validate the field, I will try subclassing the CustomNumberEditor

  6. #6

    Default

    ok, problem solved by creating the following class and registering it with initBinder
    Code:
    import org.springframework.beans.propertyeditors.CustomNumberEditor;
    import org.springframework.util.NumberUtils;
    import org.springframework.util.StringUtils;
    
    public class LongAllowsBlanksEditor extends CustomNumberEditor
    {
        public LongAllowsBlanksEditor()
        {
            super(Long.class, true);
        }
    
        /**
         * Parse the Number from the given text, using the specified NumberFormat.
         */
        public void setAsText(String text) throws IllegalArgumentException
        {
            super.setAsText(text);
            if(getValue()==null)
            {
                setValue(new Long(0));            
            }       
        }
    }

Similar Threads

  1. Submitting empty field on form
    By strbuf in forum Web
    Replies: 11
    Last Post: Aug 11th, 2005, 07:29 PM
  2. Replies: 1
    Last Post: May 29th, 2005, 07:51 PM
  3. Replies: 1
    Last Post: May 25th, 2005, 06:08 PM
  4. hidden input field for validation?
    By lanala4 in forum Web
    Replies: 2
    Last Post: May 16th, 2005, 06:54 AM
  5. Form input generation macro for password field
    By Sampo Pasanen in forum Web
    Replies: 0
    Last Post: Sep 6th, 2004, 06:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •