0

I have a DateTimeField that I am trying to allow for 2 types of formats based on the user's preferences: MM/dd/yy or dd/MM/yy. When a user clicks DatePicker and selects a date, I am able to display the date in a DateTextField with the correct formatting. However, when I try to submit the form, I get "'date' is not a valid Date" alert, which is the default alert for the DateTimeField. Is there a way I can set, not just the pattern of the date, but the validation pattern too? I've tried implementing my own IValidator and IConverter as other posts have mentioned, but I am having no luck.

Here is the code for how I am altering the way the picked date is displayed in the DateTextField:

   DateTimeField fromDate = new DateTimeField("fromDate") { 

   private static final long serialVersionUID = 1L;

        @Override 
        protected DatePicker newDatePicker() { 
            DatePicker datePicker = new DatePicker(){ 

                private static final long serialVersionUID = 1L;

                @Override 
                protected String getDatePattern() { 
                     return ((String) getSession()
                            .getAttribute("dateFormat")).replace("yyyy", "yy"); 
                } 

         }; 
         return datePicker; 
    } 

1 Answer 1

1

You should use a dateTextField with a custom converter:

new DateTimeField(..) {
    protected DateTextField newDateTextField(String id, PropertyModel<Date> dateFieldModel)
    {
        return new DateTextField(id, model, yourCustomConverter);
    }
}
1
  • Thanks for the quick reply! I was actually doing this as well, but did not include the property model in my new DateTextField. Stupid mistake on my part. This had me puzzled for quite some time! Much appreciated!
    – D2Gambit
    Commented Nov 3, 2014 at 15:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.