Go Back   SmartClient Forums > Smart GWT Technical Q&A
Wiki Register Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 27th Oct 2010, 10:08
mswiege mswiege is offline
Registered Developer
 
Join Date: May 2010
Posts: 6
Default RegExpValidator and DateItem

I'm using a DateItem with setUseTextField set to true. I'm using European date format which is "dd.MM.yyyy".
That is why I set the date formater at the beginning of my form like this:
Code:
        DateDisplayFormatter dateFormater = new DateDisplayFormatter() {
            @Override
            public String format(Date date) {
                if (date == null) return null;
                DateTimeFormat dateFormatter = DateTimeFormat.getFormat("dd.MM.yyyy");
                return dateFormatter.format(date);
            }
        };
        DateUtil.setShortDateDisplayFormatter(dateFormater);
        DateUtil.setDateInputFormatter(new DateInputFormatter()
        {
            @Override
            public Date parse(String dateString) {
                final DateTimeFormat dateFormatter = DateTimeFormat.getFormat("dd.MM.yyyy");
                Date date = dateFormatter.parse(dateString);
                return date;
            }            
        });
Now I also want to use a validator to check the input of the text field, so I created a RegExValidator. But the validator reports an invalid value even if a valid date is entered. I checked the regular expression and it should be fine.
This is my code for the DateItem:
Code:
DateItem birthDateDI = new DateItem("birthdate");
        birthDateDI.setUseTextField(true);
        birthDateDI.setValidators(new RegExpValidator("^(0?[1-9]|[12][0-9]|3[01])\\.(0?[1-9]|1[012])\\.(19|20)\\d\\d$"));
        birthDateDI.setValidateOnExit(true);
Entering for example "27.10.2010" into the text box or select a date with the calendar, results in the validator claiming the date entered is invalid.
What am I doing wrong here?
Reply With Quote
  #2  
Old 27th Oct 2010, 10:29
Isomorphic Isomorphic is offline
Administrator
 
Join Date: May 2006
Posts: 37,158
Default

Set the date input format to "DMY", and note also you don't need to set a custom function for this type of display, it's one of the default formats (note the separator char is settable).
Reply With Quote
  #3  
Old 27th Oct 2010, 10:56
mswiege mswiege is offline
Registered Developer
 
Join Date: May 2010
Posts: 6
Default

Thanks Isomorphic for your reply, but I don't get it. Where should I set the date format to DMY and the seperator char?
The only function I could find was setMaskDateSeparator(), but that had no effect.
I removed the whole lines with DateUtil functions and used this code
Code:
        birthDateDI.setUseTextField(true);
        birthDateDI.setMaskDateSeparator(".");
       birthDateDI.setDateFormatter(DateDisplayFormat.TOEUROPEANSHORTDATE);
        birthDateDI.setValidateOnExit(true);
But still the date is shown with "/" as seperator instead of "." (but the validator seems to work fine now).
Reply With Quote
  #4  
Old 27th Oct 2010, 11:06
Isomorphic Isomorphic is offline
Administrator
 
Join Date: May 2006
Posts: 37,158
Default

All the relevant APIs are on DateUtil. The API to set the separator char is recently added however, so you'd need a nightly (smartclient.com/builds).
Reply With Quote
  #5  
Old 27th Oct 2010, 11:47
mswiege mswiege is offline
Registered Developer
 
Join Date: May 2010
Posts: 6
Default

OK, thanks. Will try that...
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search


© 2010,2011 Isomorphic Software. All Rights Reserved