Announcement Announcement Module
Collapse
No announcement yet.
Command object not able to set DATE objects while populating from Form Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

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

    Comment


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

      Comment


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

        Comment

        Working...
        X