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 MaxLength extends BaseValidator |
34 |
| { |
35 |
| private int _maxLength; |
36 |
| |
37 |
0
| public MaxLength()
|
38 |
| { |
39 |
| |
40 |
| } |
41 |
| |
42 |
8
| public MaxLength(String initializer)
|
43 |
| { |
44 |
8
| super(initializer);
|
45 |
| } |
46 |
| |
47 |
8
| public void setMaxLength(int maxLength)
|
48 |
| { |
49 |
8
| _maxLength = maxLength;
|
50 |
| } |
51 |
| |
52 |
6
| public void validate(IFormComponent field, ValidationMessages messages, Object object)
|
53 |
| throws ValidatorException |
54 |
| { |
55 |
6
| String string = (String) object;
|
56 |
| |
57 |
6
| if (string.length() > _maxLength)
|
58 |
4
| throw new ValidatorException(buildMessage(messages, field),
|
59 |
| ValidationConstraint.MAXIMUM_WIDTH); |
60 |
| } |
61 |
| |
62 |
6
| protected String buildMessage(ValidationMessages messages, IFormComponent field)
|
63 |
| { |
64 |
6
| return messages.formatValidationMessage(
|
65 |
| getMessage(), |
66 |
| ValidationStrings.VALUE_TOO_LONG, |
67 |
| new Object[] |
68 |
| { new Integer(_maxLength), field.getDisplayName() }); |
69 |
| } |
70 |
| |
71 |
2
| public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
|
72 |
| FormComponentContributorContext context, IFormComponent field) |
73 |
| { |
74 |
2
| context.includeClasspathScript("/org/apache/tapestry/form/validator/StringValidator.js");
|
75 |
| |
76 |
2
| StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_max_length(event, '");
|
77 |
2
| buffer.append(field.getClientId());
|
78 |
2
| buffer.append("', ");
|
79 |
2
| buffer.append(_maxLength);
|
80 |
2
| buffer.append(", ");
|
81 |
2
| buffer.append(TapestryUtils.enquote(buildMessage(context, field)));
|
82 |
2
| buffer.append("); }");
|
83 |
| |
84 |
2
| context.addSubmitHandler(buffer.toString());
|
85 |
| } |
86 |
| |
87 |
| } |