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 | 0 | { |
44 | 0 | } |
45 | |
|
46 | |
public MaxDate(String initializer) |
47 | |
{ |
48 | 0 | super(initializer); |
49 | 0 | } |
50 | |
|
51 | |
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
52 | |
throws ValidatorException |
53 | |
{ |
54 | 0 | Date date = (Date) object; |
55 | 0 | DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class); |
56 | |
|
57 | 0 | if (date.after(_maxDate)) |
58 | 0 | throw new ValidatorException(buildMessage(messages, field, translator), |
59 | |
ValidationConstraint.TOO_LARGE); |
60 | |
|
61 | 0 | } |
62 | |
|
63 | |
private String buildMessage(ValidationMessages messages, IFormComponent field, |
64 | |
DateTranslator translator) |
65 | |
{ |
66 | 0 | 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 | 0 | DateTranslator translator = (DateTranslator) getFieldTranslator(field, DateTranslator.class); |
82 | 0 | if (translator == null) |
83 | 0 | return; |
84 | |
|
85 | 0 | JSONObject profile = context.getProfile(); |
86 | |
|
87 | 0 | context.addInitializationScript(field, "dojo.require(\"tapestry.form.datetime\");"); |
88 | |
|
89 | 0 | if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
90 | 0 | profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
91 | |
} |
92 | 0 | JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
93 | |
|
94 | 0 | 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 | 0 | accumulateProfileProperty(field, profile, |
105 | |
ValidationConstants.CONSTRAINTS, buildMessage(context, field, translator)); |
106 | 0 | } |
107 | |
|
108 | |
public void setMaxDate(Date minDate) |
109 | |
{ |
110 | 0 | _maxDate = minDate; |
111 | 0 | } |
112 | |
|
113 | |
public Date getMaxDate() |
114 | |
{ |
115 | 0 | return _maxDate; |
116 | |
} |
117 | |
} |