Results 1 to 3 of 3

Thread: Binding several input fields straight into a java.util.Date

  1. #1
    Join Date
    Feb 2007
    Posts
    21

    Thumbs up Binding several input fields straight into a java.util.Date

    Dear All,

    I want to share with you my findings on the topic of binding data originating from different HTML inputs straight into a java.util.Date.

    It does not require any javascript, any new PropertyEditor, anything... Hopefully you will also like it!!!!

    Let's consider an example. Your HTML view has:
    • an input for the day where you expect the user to enter DD/MM/YYYY
    • a drop down for selecting hours
    • a drop down for selecting minutes


    Your form object has a java.util.Date attribute called travelDate.

    The binding
    1. By naming all the HTML elements after the attribute name: travelDate
    2. Register a CustomDateEditor in your Controller (binder.registerCustomEditor) with a comma separated list of patterns that match in order the appearance of your HTML input fields. This is the trick!!


    Spring will invoke the setAsText of your CustomDateEditor by passing the user input in the form of comma separated string. For instance '07/02/2007,14,37'.

    This will in turn be parsed as a date.

    (see related thread: Binding composite properties with DataBinder)

  2. #2

    Default

    A potential risk is that the order of the comma separated values may not always be consistent.

    If the JSP developer hides the time fields or moves them around, there's a risk that the information cannot be parsed.

    Perhaps if the CustomDateEditor used a more advanced DateFormatter that used regular expressions, allowing missing times, or values shuffled around.

  3. #3
    Join Date
    Feb 2007
    Posts
    21

    Default True...

    Well the order of fields is changed in the JSP it will have to be reflected in the pattern.

    When having several HTML form elements with the same name attribute:
    Code:
    <input name="birthDate" ...
    
    <select name="birthDate" ...
    
    <select name="birthDate" ...
    the request.getParameter("birthDate") will always return an array of String[] with a length equal to the number of input fields.

    For the date example before, if all fields are missing you get back:
    ",,"

    If let's say the hour input is missing, you would get back:
    "12/02/2007,,23"

    A pattern "dd/MM/yyyy,HH,mm" will fail to parse the previous inputs.

    The drawback of this approach is not being able to tell the user exactly what field was missing. Moreover you cannot tell the difference (unless you write additional code) between an incorrectly formatted input (e.g. aaaa/10/2007) and a missing field.

    In our project it is not an issue. We will display a generic error such as "Missing date, hour or time" as the date will be provided by a popup calendar, the hour and minutes by drop downs.

Posting Permissions

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