1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.form.validator; |
16 |
|
|
17 |
|
import java.util.Date; |
18 |
|
|
19 |
|
import org.apache.tapestry.IMarkupWriter; |
20 |
|
import org.apache.tapestry.IRequestCycle; |
21 |
|
import org.apache.tapestry.form.FormComponentContributorContext; |
22 |
|
import org.apache.tapestry.form.IFormComponent; |
23 |
|
import org.apache.tapestry.form.ValidationMessages; |
24 |
|
import org.apache.tapestry.form.translator.DateTranslator; |
25 |
|
import org.apache.tapestry.json.JSONLiteral; |
26 |
|
import org.apache.tapestry.json.JSONObject; |
27 |
|
import org.apache.tapestry.valid.ValidationConstants; |
28 |
|
import org.apache.tapestry.valid.ValidationConstraint; |
29 |
|
import org.apache.tapestry.valid.ValidationStrings; |
30 |
|
import org.apache.tapestry.valid.ValidatorException; |
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
|
38 |
|
public class MaxDate extends BaseValidator |
39 |
|
{ |
40 |
|
private Date _maxDate; |
41 |
|
|
42 |
|
public MaxDate() |
43 |
2 |
{ |
44 |
2 |
} |
45 |
|
|
46 |
|
public MaxDate(String initializer) |
47 |
|
{ |
48 |
3 |
super(initializer); |
49 |
3 |
} |
50 |
|
|
51 |
|
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
52 |
|
throws ValidatorException |
53 |
|
{ |
54 |
3 |
Date date = (Date) object; |
55 |
4 |
DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class); |
56 |
|
|
57 |
3 |
if (date.after(_maxDate)) |
58 |
2 |
throw new ValidatorException(buildMessage(messages, field, translator), |
59 |
|
ValidationConstraint.TOO_LARGE); |
60 |
|
|
61 |
1 |
} |
62 |
|
|
63 |
|
private String buildMessage(ValidationMessages messages, IFormComponent field, |
64 |
|
DateTranslator translator) |
65 |
|
{ |
66 |
4 |
return messages.formatValidationMessage( |
67 |
|
getMessage(), |
68 |
|
ValidationStrings.DATE_TOO_LATE, |
69 |
|
new Object[] |
70 |
|
{ field.getDisplayName(), |
71 |
|
(translator != null) ? |
72 |
|
translator.format(field, messages.getLocale(), _maxDate) |
73 |
|
: _maxDate.toString()}); |
74 |
|
} |
75 |
|
|
76 |
|
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
77 |
|
FormComponentContributorContext context, IFormComponent field) |
78 |
|
{ |
79 |
|
|
80 |
|
|
81 |
2 |
DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class); |
82 |
2 |
if (translator == null) |
83 |
0 |
return; |
84 |
|
|
85 |
2 |
JSONObject profile = context.getProfile(); |
86 |
|
|
87 |
2 |
context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");"); |
88 |
|
|
89 |
2 |
if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
90 |
2 |
profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
91 |
|
} |
92 |
2 |
JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
93 |
|
|
94 |
2 |
accumulateProperty(cons, field.getClientId(), |
95 |
|
new JSONLiteral("[tapestry.form.datetime.isValidDate,{" |
96 |
|
+ "max:" |
97 |
|
+ JSONObject.quote(translator.format(field, context.getLocale(), _maxDate)) |
98 |
|
+ "," |
99 |
|
+ "datePattern:" |
100 |
|
+ JSONObject.quote(translator.getPattern()) |
101 |
|
+ (translator.isLenient() ? "" : ",strict:true") |
102 |
|
+ "}]")); |
103 |
|
|
104 |
2 |
accumulateProfileProperty(field, profile, |
105 |
|
ValidationConstants.CONSTRAINTS, buildMessage(context, field, translator)); |
106 |
2 |
} |
107 |
|
|
108 |
|
public void setMaxDate(Date minDate) |
109 |
|
{ |
110 |
5 |
_maxDate = minDate; |
111 |
5 |
} |
112 |
|
|
113 |
|
public Date getMaxDate() |
114 |
|
{ |
115 |
0 |
return _maxDate; |
116 |
|
} |
117 |
|
} |