Announcement Announcement Module
Collapse
No announcement yet.
Spring validation Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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

      Comment


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

        Comment

        Working...
        X