Results 1 to 4 of 4

Thread: Command object not able to set DATE objects while populating from Form

  1. #1
    Join Date
    Aug 2008
    Location
    Bangalore
    Posts
    11

    Default Command object not able to set DATE objects while populating from Form

    Dear all,
    I've two Date fields in my form.I'm extending SimpleFormController.I've a command object for this form which is throwing a validation error when submitting the form.
    Here is the error

    Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'estimatedCompletionDate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'estimatedCompletionDate': no matching editors or conversion strategy found
    Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'projectStartDate'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.lang.String] to required type [java.util.Date] for property 'projectStartDate': no matching editors or conversion strategy found


    Is there anyway to set automatically to the Date Object type or we need to manually convert it as DATE object ?
    Does anyone has any idea on this? Please let me know.

    Thanks,
    Sekhar.

  2. #2
    Join Date
    Jan 2007
    Location
    Orlando, FL USA
    Posts
    84

    Default

    Hello Sekhar,

    Try override initBinder method in your controller, for example like this:
    Code:
    @override
    protected void initBinder(HttpServletRequest request,
                              ServletRequestDataBinder binder)
                       throws Exception{
    
      binder.registerCustomEditor(new org.springframework.beans.propertyeditors.CustomDateEditor(new java.text.SimpleDateFormat("YYYY-MM-DD"), false));
    }
    you can change the format of date to your liking. The false parameter means you allow empty values in your date form. See API for more details.

    -Z
    http://www.jroller.com/thebugslayer - notes on java, scala and other development stuff.

  3. #3
    Join Date
    Aug 2008
    Location
    Bangalore
    Posts
    11

    Default

    Thanks a lot thebugsplayer..!!

    How about if he doesn't enter any date in the form form validation is failing. I'm getting exception like this
    <!--
    Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'projectStartDate'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: ""
    -->
    If anybody has any idea on this ?

    Thanks,
    Sekhar.

  4. #4
    Join Date
    Nov 2005
    Posts
    114

    Default

    The data binder normally will convert the exception to a typeMismatch error.
    One quick way to handle this is (assuming you have an error messages properties file) to add an entry like:

    typeMismatch=Invalid data entry.

Posting Permissions

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