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