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