Uploaded image for project: 'Tapestry 5'
  1. Tapestry 5
  2. TAP5-1998

DateField does not validate dates properly

    Details

      Description

      Due to the way the Java DateFormat class works there are 2 cases that allow invalid dates threw the DateField component.

      1. By default date parsing is lenient. This means dates such as 00/01/2000 are converted to 12/01/1999 and passed thru. There is no way for the application to know this has happened.

      2. The DateFormat class ignores trailing characters. This means an input of 01/01/200- will result in a date of 01/01/200 again there is no way for the application to know this has happened.

      It's possible to work around both these issues but I think the workarounds are not obvious and in practice difficult to implement. To work around the first problem you can pass a DateFormat with lenient set to false. The service that converts Strings to DateFormat does not do this and it does not seem possible to override it so all the DateFormats much be created in Java code. The second issue can be resolved by providing a regexp expression to validate the date. For some date formats such as MM/dd/yyyy this is pretty easy but other date formats are complex and it's difficult to come up with a correct regular expression.

      So I would like to propose changing DateField to set lenient to false and use parse(source,pos) instead of paste(source). This change is not strictly backward compatible and a parameter could be added that allows the old behavior. My guess is there are not many people depending on the existing behavior since it really allows invalid dates to be silently input.

      I have a patch that I can contribute.

        Activity

        Hide
        thiagohp Thiago H. de Paula Figueiredo added a comment -

        Hi, Barry!

        I really cannot figure how we could use DateFormat.parse(String, ParsePosition) here. What ParsePosition value would we use? I have absolutely no idea.

        I'll commit a fix that adds the SymbolConstants.LENIENT_DATE_FORMAT (tapestry.lenient-date-format) symbol with a default value of false. In addition, DateField gets a 'lenient' parameter that takes the symbol as its default value.

        Cheers!

        Thiago

        Show
        thiagohp Thiago H. de Paula Figueiredo added a comment - Hi, Barry! I really cannot figure how we could use DateFormat.parse(String, ParsePosition) here. What ParsePosition value would we use? I have absolutely no idea. I'll commit a fix that adds the SymbolConstants.LENIENT_DATE_FORMAT (tapestry.lenient-date-format) symbol with a default value of false. In addition, DateField gets a 'lenient' parameter that takes the symbol as its default value. Cheers! Thiago
        Hide
        jira-bot ASF subversion and git services added a comment -

        Commit 913c80b012c780f3de6e03c3471358e053217d87 in branch refs/heads/master from Thiago H. de Paula Figueiredo
        [ https://git-wip-us.apache.org/repos/asf?p=tapestry-5.git;h=913c80b ]

        TAP5-1998. DateField does not validate dates properly. From now on,
        Tapestry will use non-lenient DateFields unless configured otherwise,
        through the SymbolConstants.LENIENT_DATE_FORMAT symbol or the DateField
        'lenient' parameter.

        Show
        jira-bot ASF subversion and git services added a comment - Commit 913c80b012c780f3de6e03c3471358e053217d87 in branch refs/heads/master from Thiago H. de Paula Figueiredo [ https://git-wip-us.apache.org/repos/asf?p=tapestry-5.git;h=913c80b ] TAP5-1998 . DateField does not validate dates properly. From now on, Tapestry will use non-lenient DateFields unless configured otherwise, through the SymbolConstants.LENIENT_DATE_FORMAT symbol or the DateField 'lenient' parameter.
        Hide
        jkemnade Jochen Kemnade added a comment -

        It seems as if this change causes org.apache.tapestry5.integration.app1.FormTests.basic_datefield() to fail. The test does not fill in the required lenient form field.

        Show
        jkemnade Jochen Kemnade added a comment - It seems as if this change causes org.apache.tapestry5.integration.app1.FormTests.basic_datefield() to fail. The test does not fill in the required lenient form field.

          People

          • Assignee:
            thiagohp Thiago H. de Paula Figueiredo
            Reporter:
            trsvax Barry Books
          • Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development