Tapestry
  1. Tapestry
  2. TAPESTRY-904

DatePicker SimpleDateParser needs "strict" parser

    Details

    • Type: Improvement Improvement
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 4.1
    • Fix Version/s: 4.1.1
    • Component/s: Framework
    • Labels:
      None

      Description

      The datepicker is accepting dates such as 04/99/2005 The translator converts this to 07/08/2006 I looked at the code and it is using SimpleDateFormat() without a strict parser.
      Perhaps, the datepicker can accept an optional parameter that would control if the SimpleDateFormat is true/false?

      Thanks.

        Activity

        Hide
        Jesse Kuhnert added a comment -

        added a "lenient" parameter to the date translator.

        Show
        Jesse Kuhnert added a comment - added a "lenient" parameter to the date translator.
        Hide
        Greg Woolsey added a comment -

        Here's what we've created in 4.1.1 for use in our app:

        public class StrictDateTranslator extends DateTranslator {
        public StrictDateTranslator()

        { super(); }

        public StrictDateTranslator(String initializer)

        { super(initializer); }

        @Override
        protected String defaultPattern()

        { return "MM/dd/yy"; }

        @Override
        protected String getMessageKey()

        { "invalid-date"; }

        @Override
        protected String buildMessage(ValidationMessages messages, IFormComponent field, String key)

        { String label = field.getDisplayName(); Object[] parameters = getMessageParameters(messages.getLocale(), label); return messages.formatValidationMessage(field.getMessages().getMessage(key), key, parameters); }

        @Override
        public SimpleDateFormat getDateFormat(Locale locale)

        { return new StrictDateFormat(getPattern(), new DateFormatSymbols(locale)); }

        /**

        • @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale,
        • java.lang.String)
          */
          @Override
          protected Object[] getMessageParameters(Locale locale, String label)
          {
          String pattern = getDateFormat(locale).toLocalizedPattern().toUpperCase(locale);
          pattern += "[YY]";

        return new Object[]

        { label, pattern }

        ;
        }

        Where StrictDateFormat validates that the entire input string is part of the parsed date:

        public class StrictDateFormat extends SimpleDateFormat {
        public StrictDateFormat(String pattern, DateFormatSymbols formatSymbols)

        { super(pattern, formatSymbols); setLenient(false); }

        @Override
        public Object parseObject(String source) throws ParseException {
        ParsePosition pos = new ParsePosition(0);
        Object result = parseObject(source, pos);
        if (pos.getIndex() != source.length())

        { throw new ParseException("Format.parseObject(String) failed", pos.getErrorIndex()); }

        return result;
        }
        }

        Show
        Greg Woolsey added a comment - Here's what we've created in 4.1.1 for use in our app: public class StrictDateTranslator extends DateTranslator { public StrictDateTranslator() { super(); } public StrictDateTranslator(String initializer) { super(initializer); } @Override protected String defaultPattern() { return "MM/dd/yy"; } @Override protected String getMessageKey() { "invalid-date"; } @Override protected String buildMessage(ValidationMessages messages, IFormComponent field, String key) { String label = field.getDisplayName(); Object[] parameters = getMessageParameters(messages.getLocale(), label); return messages.formatValidationMessage(field.getMessages().getMessage(key), key, parameters); } @Override public SimpleDateFormat getDateFormat(Locale locale) { return new StrictDateFormat(getPattern(), new DateFormatSymbols(locale)); } /** @see org.apache.tapestry.form.translator.AbstractTranslator#getMessageParameters(java.util.Locale, java.lang.String) */ @Override protected Object[] getMessageParameters(Locale locale, String label) { String pattern = getDateFormat(locale).toLocalizedPattern().toUpperCase(locale); pattern += " [YY] "; return new Object[] { label, pattern } ; } Where StrictDateFormat validates that the entire input string is part of the parsed date: public class StrictDateFormat extends SimpleDateFormat { public StrictDateFormat(String pattern, DateFormatSymbols formatSymbols) { super(pattern, formatSymbols); setLenient(false); } @Override public Object parseObject(String source) throws ParseException { ParsePosition pos = new ParsePosition(0); Object result = parseObject(source, pos); if (pos.getIndex() != source.length()) { throw new ParseException("Format.parseObject(String) failed", pos.getErrorIndex()); } return result; } }

          People

          • Assignee:
            Jesse Kuhnert
            Reporter:
            phillip rhodes
          • Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development