Announcement Announcement Module

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions ( project, this forum has been decommissioned in favour of Spring Extensions one at:

Please see the Spring Extensions home page for a complete list of current projects in Java, .NET and ActionScript. You can also propose one if you want.

Costin Leau
SpringSource - Spring Training, Consulting, and Support - "From the Source"
See more
See less
[spring-modules-validation] how to validate java.lang.Date Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • [spring-modules-validation] how to validate java.lang.Date

    I have a class with the following fields:
    @Length(min = 2)
    private String forename;
    @Length(min = 2)
    private String surname;
    private String birthday;
    With this definitions I have 2 problems:

    1) birthday: This field should be of the type Date, and not of the type String, but if I change the data type, the validation process causes an Exception, that means that he can't validate this data type.

    2) Second one; how can I trim the values in the <form></form>, before they are set, and before the 'filled' is validated.

    Any suggestions or links, any help is welcome?


  • #2
    You need to define or write a CustomDateEditor so Date's can work.

    Since you did not post any info on the Exception, it is difficult to ascertain exactly what is wrong;
    but most likely it is due to the fact that a property editor for Date objects is not registered by default
    like most other standard types; this is due to the fact the you must provide a format for date
    parsing/rendering and that format is locale and application specific.

    Section of the Spring Docs html reference (v2.5.6) describes how to define the needed
    format and register the CustomEditor() using @InitBinder annotation.

    However, when a Date propertyEditor is registered, you need to recognize that date validation is done
    by the propertyEditor as part of the binding step, BEFORE any validator gets called, because Spring
    implements bind then validate and there is no way to have declarative validation done on the incoming
    text from the form POST, unless you intercept it ahead of binding by overriding the onBind() method
    of SimpleFormController, etc. and write java code to do the validation before the CustomEditor does

    This is logical, as the Date propertyEditor has to parse the date sub fields and verify their validity if
    it is going to produce a valid Date object to bind to the formBackingObject. This characteristic is unique
    to the Date binding, due to this inherent nature of date representation.

    The CustomDateEditor is a limited parsing technique, because of the its "regular expression" based
    parsing approach for input and as a result all sorts of poor UI date input "rules" result from using
    the stock CustomDateEditor with a SimpleDateFormat format "specification".

    The most common problems are 1) that the sub fields can NOT be variable length and 2) the year
    being entered as two digit rather than four creates undesirable results. Normal users, and especially
    business application users want the be able to type 2/2/2 and get the date 02/02/2002,
    instead of having to type 02/02/2002, which of course has as many zero's as 2's.

    I implemented a BasicDateEditor that only does dates, but not time or any thing else like time zone or
    months as letters; it allows only digits and delimiters and always get the correct year and allows a set of
    delimiters ( ",-/.") all of which are allowed (because they are all un-shifted on the keyboard) and then
    converts any of the entry delimiters to the one specified by the given output format, before then converting
    to a Date object. I allows 1,1,1 or 1,2.3 or 02/02/2002, and verifies that the MM and dd are 1/2 digits, and
    has 1/2/4 digits for the year, and requires all three sub fields be entered. The BasicDateEditor only
    allows dates within the range -80 thru +20 years of the current year if a 1/2 digit year is entered.

    Because the "binder" is really doing the date entry validation, the only kind of validation
    the Bean Validation Framework (and others) support is checking the value of the date relative to the
    current date AFTER a valid Date object has been created. Thus there are @InThePast and @InTheFuture
    validation rules.

    I can post the source for BasicDateEditor if anyone wants to use it or use it to create one themselves.
    Last edited by exAppl088; Jan 13th, 2009, 03:24 AM. Reason: corrected stmt about zero's and 2's


    • #3
      Date field validation in spring

      Hi All,

      I'm facing a validation problem in my form.

      I have 3 fields in my form[Eg: 1)File ref Id 2)From date 3)To date].first field of type String and another 2 fields of type java.util.Date.As per our business validation rules either user has to enter either first field or the remaining two date (date range eg: from date and to date)fields.I have registered CustomDate Editor in my SimpleFormController.

      I have one Validator class also.If the user enters first field the validation for Date fields should be skipped.If the user enters only one date field(say From date), an error message should be displayed "To Date is required".

      If the user dont enter any of the fields, an error message should be displayed "Either either File ref Id or date range i.e from date and to date".

      The problem here is if the user enters File ref id and click submit button, as the date fields is of type java.util.Date in the commandBean and as I have CustomDate editor,the Errors object in the validate method still contains errors.and my onSubmit() method is not getting invoked.

      Any help would be appriciated.

      Thanks in Adavance.