Announcement Announcement Module
Collapse
No announcement yet.
server-side date validatiion with Commons validator? funny? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • server-side date validatiion with Commons validator? funny?

    hi, i have encountered an interesting situation regarding
    server-side date validation using Commons validator.

    the form has a date field called startDate that requires
    input with format MM/dd/yyyy. i added a CustomDateEditor
    in the form controller. my command object has a startDate date
    field.

    i input a date with the format MM/dd/yyyy, Spring did
    not report any problem, but Commons validator, called
    after Spring's data binding, complains
    about startDate being not a date field. this is funny because
    Spring has already parsed the input correctly. then what
    is the point of calling Commons date validator afterwards?
    Note that Spring calls its validator after data binding.

    what is even interesting to me is that my Common validtor
    expects "MM/dd/yyyy" input. after Spring binds date
    correctly, my Common validator complained because it
    did not get "MM/dd/yyyy" format string from the command object.

    i think Spring web needs to allow validator be called before
    its data binding and this "before data binding" or "after"
    should be configurable. does Spring already has this feature?

    i am not an expert on Spring, simply based on reading source code,
    doc, and testing. please correct me if i am wrong.

    i got to say that the learning curve is steeeeeeeeeep.

    thanks, pete



    there is an interesting and funny design

  • #2
    Validation happens on the domain object, so I don't think 'before' validation makes sense, does it... If validation wouuld happen before binding, what would the subject of validation be (the servlet request?).

    Also, the object you're passing to the commons validator is one having a java.util.Date property, isn't it? I believe this should just work. Any more info on the validator (logs or anything)?

    regards,
    Alef Arendsen

    Comment


    • #3
      Are you sure Spring is giving it mm/dd/yyyy, and not mm/dd/yy?

      The Custom Editor takes a Dateformat object, and off the top of my head I think Dateformat.medium (or is it short?), only has 2 places for the year.

      I may be completly wrong, but it's something to take a look at.

      Comment


      • #4
        Alef,

        Thanks for your input. I checked my code and you are right. I found
        a bug in my code and the following is the correct piece in initBinder:

        SimpleDateFormat dateFormat1 = new SimpleDateFormat("MM/dd/YYYY");
        dateFormat1.setLenient(false);
        binder.registerCustomEditor(Date.class, startDate, new CustomDateEditor(
        dateFormat1, true));

        Now if I input 04/05/2005, it passes both the controller and the Commons
        validator. However, you may be interested in the following tests:

        If I input 04/05/2005 02:03:04, it also passes both, why? The Commons
        validator is also configured to accept MM/dd/yyyy dates.

        If I input 04/05/2005 xyzabc, it also passes both. Strange?

        If I input xyzabc, then I get an error message as follows:

        Failed to convert property value of type [java.lang.String] to required type [java.util.Date] for property 'startDate'; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "xyzabc"

        Thanks, Pete

        Comment

        Working...
        X