Results 1 to 4 of 4

Thread: Date field validation in spring mvc

  1. #1
    Join Date
    Jun 2010
    Posts
    3

    Default 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. #2
    Join Date
    Nov 2009
    Location
    Amsterdam, The Netherlands
    Posts
    54

    Default

    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?

  3. #3
    Join Date
    Jun 2010
    Posts
    3

    Default

    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

  4. #4
    Join Date
    Nov 2009
    Location
    Amsterdam, The Netherlands
    Posts
    54

    Default

    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.

Posting Permissions

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