1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.exoplatform.ecm.webui.form.validator;
18
19 import java.text.DateFormat;
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22
23 import org.exoplatform.web.application.ApplicationMessage;
24 import org.exoplatform.webui.application.WebuiRequestContext;
25 import org.exoplatform.webui.core.UIComponent;
26 import org.exoplatform.webui.exception.MessageException;
27 import org.exoplatform.webui.form.UIForm;
28 import org.exoplatform.webui.form.UIFormDateTimeInput;
29 import org.exoplatform.webui.form.UIFormInput;
30 import org.exoplatform.webui.form.validator.DateTimeValidator;
31
32
33
34
35
36
37
38
39 public class DateValidator extends DateTimeValidator {
40
41 @SuppressWarnings("unchecked")
42 public void validate(UIFormInput uiInput) throws Exception {
43 String inputValue = ((String)uiInput.getValue());
44 if (inputValue.trim().indexOf(" ") > 0) {
45 super.validate(uiInput);
46 } else {
47 validateDateFormat(uiInput);
48 }
49 }
50
51 private void validateDateFormat(UIFormInput uiInput) throws Exception {
52
53 String inputValue = ((String)uiInput.getValue());
54 try {
55 WebuiRequestContext requestContext = WebuiRequestContext.getCurrentInstance();
56 DateFormat dateFormat_ = SimpleDateFormat.getDateInstance(DateFormat.SHORT, requestContext.getLocale());
57 dateFormat_.setLenient(false);
58 dateFormat_.parse(inputValue);
59 ((UIFormDateTimeInput)uiInput).setValue(inputValue + " 00:00:00");
60 } catch (ParseException parseEx) {
61
62 UIComponent uiComponent = (UIComponent) uiInput ;
63 UIForm uiForm = uiComponent.getAncestorOfType(UIForm.class) ;
64 String label;
65 try{
66 label = uiForm.getLabel(uiInput.getName());
67 } catch(Exception ex) {
68 label = uiInput.getName();
69 }
70 label = label.trim();
71 if(label.charAt(label.length() - 1) == ':') label = label.substring(0, label.length() - 1);
72
73
74 throw new MessageException(new ApplicationMessage("DateTimeValidator.msg.Invalid-input",
75 new Object[] { label, inputValue }));
76 }
77 }
78
79 }