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