Results 1 to 4 of 4

Thread: Spring validation

Hybrid View

  1. #1
    Join Date
    Nov 2009
    Posts
    15

    Default Spring validation

    Hi all,

    What is the way to check a bad format from a form ?

    I have this:
    Code:
    public class Profil {
      @Notnull(message="Must be not null")
      private Double weight;
    
    ...getset
    }
    But if my user put a string into the weight field, I get this:
    Code:
    Failed to convert property value of type java.lang.String to required type java.lang.Double for property weight; nested exception is org.springframework.core.convert.ConversionFailedException: Unable to convert value zd from type java.lang.String to type java.lang.Double; nested exception is java.lang.NumberFormatException: For input string: "zd"
    I didn't find the tips to display an appropriate message.

    Thanks

  2. #2

    Default

    The problem here isn't with validation, it is with the data binding. Data binding happens before validation, and validation is only invoked when all fields have been converted and bound correctly. In your case, the binding step is failing because the String value cannot be converted to a Double.

    To customize the error message from the bind failure, declare a MessageSource bean in your application context and create a messages.properties resource bundle as described here: http://static.springsource.org/sprin...-messagesource.

    To specify the message for a data binding error like this, specify a message in the resource bundle with one of the following keys, depending on how specific you want to get: typeMismatch, typeMismatch.profil.weight, typeMismatch.weight, typeMismatch.java.lang.Double.

  3. #3

    Default

    Quote Originally Posted by scottyfred View Post
    The problem here isn't with validation, it is with the data binding. Data binding happens before validation, and validation is only invoked when all fields have been converted and bound correctly. In your case, the binding step is failing because the String value cannot be converted to a Double.

    To customize the error message from the bind failure, declare a MessageSource bean in your application context and create a messages.properties resource bundle as described here: http://static.springsource.org/sprin...-messagesource.

    To specify the message for a data binding error like this, specify a message in the resource bundle with one of the following keys, depending on how specific you want to get: typeMismatch, typeMismatch.profil.weight, typeMismatch.weight, typeMismatch.java.lang.Double.
    Hi Scottyfred,

    Thanks for your advise. I am a newbie here and I had a similar problem. But with your advise now a start closer...

    Happy Easter

    Ken
    “Life is a journey, not a destintion” Aerosmith
    http://www.understandhairloss.org

  4. #4

    Default

    Above posts are correct that these errors are not at time of validation , but they are binding errors. Since the value from request comes as string and we have bind that string to double, results into this exception. So It will be great to use String in place of double and convert this string into double in the target controller.

    Hope this helps.

Posting Permissions

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