[ 
http://issues.apache.org/jira/browse/TAPESTRY-904?page=comments#action_12457158 
] 
            
Greg Woolsey commented on TAPESTRY-904:
---------------------------------------

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;
    }
}


> DatePicker SimpleDateParser needs "strict" parser
> -------------------------------------------------
>
>                 Key: TAPESTRY-904
>                 URL: http://issues.apache.org/jira/browse/TAPESTRY-904
>             Project: Tapestry
>          Issue Type: Improvement
>          Components: Framework
>    Affects Versions: 4.1
>            Reporter: phillip rhodes
>         Assigned To: Jesse Kuhnert
>            Priority: Minor
>             Fix For: 4.1.1
>
>
> 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.

-- 
This message is automatically generated by JIRA.
-
If you think it was sent incorrectly contact one of the administrators: 
http://issues.apache.org/jira/secure/Administrators.jspa
-
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to