1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.form.validator; |
16 |
| |
17 |
| import org.apache.tapestry.IMarkupWriter; |
18 |
| import org.apache.tapestry.IRequestCycle; |
19 |
| import org.apache.tapestry.TapestryUtils; |
20 |
| import org.apache.tapestry.form.FormComponentContributorContext; |
21 |
| import org.apache.tapestry.form.IFormComponent; |
22 |
| import org.apache.tapestry.form.ValidationMessages; |
23 |
| import org.apache.tapestry.valid.ValidationConstraint; |
24 |
| import org.apache.tapestry.valid.ValidationStrings; |
25 |
| import org.apache.tapestry.valid.ValidatorException; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public class Max extends BaseValidator |
34 |
| { |
35 |
| private double _max; |
36 |
| |
37 |
0
| public Max()
|
38 |
| { |
39 |
| } |
40 |
| |
41 |
10
| public Max(String initializer)
|
42 |
| { |
43 |
10
| super(initializer);
|
44 |
| } |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
6
| public void validate(IFormComponent field, ValidationMessages messages, Object object)
|
51 |
| throws ValidatorException |
52 |
| { |
53 |
6
| Number value = (Number) object;
|
54 |
| |
55 |
6
| if (value.doubleValue() > _max)
|
56 |
2
| throw new ValidatorException(buildMessage(messages, field),
|
57 |
| ValidationConstraint.TOO_LARGE); |
58 |
| } |
59 |
| |
60 |
6
| private String buildMessage(ValidationMessages messages, IFormComponent field)
|
61 |
| { |
62 |
6
| return messages.formatValidationMessage(
|
63 |
| getMessage(), |
64 |
| ValidationStrings.VALUE_TOO_LARGE, |
65 |
| new Object[] |
66 |
| { field.getDisplayName(), new Double(_max) }); |
67 |
| } |
68 |
| |
69 |
4
| public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
|
70 |
| FormComponentContributorContext context, IFormComponent field) |
71 |
| { |
72 |
4
| context.includeClasspathScript("/org/apache/tapestry/form/validator/NumberValidator.js");
|
73 |
| |
74 |
4
| String message = TapestryUtils.enquote(buildMessage(context, field));
|
75 |
| |
76 |
4
| StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_max_number(event, '");
|
77 |
4
| buffer.append(field.getClientId());
|
78 |
4
| buffer.append("', ");
|
79 |
4
| buffer.append(_max);
|
80 |
4
| buffer.append(", ");
|
81 |
4
| buffer.append(message);
|
82 |
4
| buffer.append("); }");
|
83 |
| |
84 |
4
| context.addSubmitHandler(buffer.toString());
|
85 |
| } |
86 |
| |
87 |
10
| public void setMax(double max)
|
88 |
| { |
89 |
10
| _max = max;
|
90 |
| } |
91 |
| |
92 |
| } |