Announcement Announcement Module
Collapse

Spring Modules forum decommissioned in favor of Spring Extensions

As the Spring Modules project has been replaced by the Spring Extensions (http://www.springsource.org/extensions) project, this forum has been decommissioned in favour of Spring Extensions one at:
http://forum.springsource.org/forumdisplay.php?f=44

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.

Cheers,
Costin Leau
SpringSource - http://www.SpringSource.com- Spring Training, Consulting, and Support - "From the Source"
http://twitter.com/costinl
See more
See less
Date field validation in spring mvc Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Date field validation in spring mvc

    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.

    Rajesh

  • #2
    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.
    Is the CustomDateEditor configured to allow empty fields?

    Comment


    • #3
      Hi ,
      Thanks for your reply.

      The code is provided in CustomDateEditor in such way that, if the date field is empty, the field value is set to null.

      plz refer to below code snippet from CustomDateEditor :

      public void setAsText(String text) throws IllegalArgumentException {
      if (this.allowEmpty && !StringUtils.hasText(text)) {
      // Treat empty String as null value.
      setValue(null);
      }

      Thanks,
      Rajesh

      Comment


      • #4
        It really depends on how you initialize the allowEmpty variable, doesnt it?
        The editor will throw a IllegalArgumentException if the editor doesnt allow empty fields.

        That is why I asked how the CustomDateEditor is initialized.

        Comment

        Working...
        X