1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.valid; |
16 |
| |
17 |
| import java.util.HashMap; |
18 |
| import java.util.Map; |
19 |
| |
20 |
| import org.apache.tapestry.IMarkupWriter; |
21 |
| import org.apache.tapestry.IRequestCycle; |
22 |
| import org.apache.tapestry.form.IFormComponent; |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| public class StringValidator extends BaseValidator |
33 |
| { |
34 |
| private int _minimumLength; |
35 |
| |
36 |
| private String _minimumLengthMessage; |
37 |
| |
38 |
| |
39 |
| |
40 |
| private String _scriptPath = "/org/apache/tapestry/valid/StringValidator.script"; |
41 |
| |
42 |
9
| public StringValidator()
|
43 |
| { |
44 |
| } |
45 |
| |
46 |
| |
47 |
| |
48 |
| |
49 |
| |
50 |
| |
51 |
| |
52 |
0
| public StringValidator(String initializer)
|
53 |
| { |
54 |
0
| super(initializer);
|
55 |
| } |
56 |
| |
57 |
2
| public String toString(IFormComponent field, Object value)
|
58 |
| { |
59 |
2
| if (value == null)
|
60 |
1
| return null;
|
61 |
| |
62 |
1
| return value.toString();
|
63 |
| } |
64 |
| |
65 |
7
| public Object toObject(IFormComponent field, String input) throws ValidatorException
|
66 |
| { |
67 |
7
| if (checkRequired(field, input))
|
68 |
2
| return null;
|
69 |
| |
70 |
3
| if (_minimumLength > 0 && input.length() < _minimumLength)
|
71 |
1
| throw new ValidatorException(buildMinimumLengthMessage(field),
|
72 |
| ValidationConstraint.MINIMUM_WIDTH); |
73 |
| |
74 |
2
| return input;
|
75 |
| } |
76 |
| |
77 |
0
| public int getMinimumLength()
|
78 |
| { |
79 |
0
| return _minimumLength;
|
80 |
| } |
81 |
| |
82 |
4
| public void setMinimumLength(int minimumLength)
|
83 |
| { |
84 |
4
| _minimumLength = minimumLength;
|
85 |
| } |
86 |
| |
87 |
| |
88 |
| |
89 |
| |
90 |
| |
91 |
0
| public void renderValidatorContribution(IFormComponent field, IMarkupWriter writer,
|
92 |
| IRequestCycle cycle) |
93 |
| { |
94 |
0
| if (!isClientScriptingEnabled())
|
95 |
0
| return;
|
96 |
| |
97 |
0
| if (!(isRequired() || _minimumLength > 0))
|
98 |
0
| return;
|
99 |
| |
100 |
0
| Map symbols = new HashMap();
|
101 |
| |
102 |
0
| if (isRequired())
|
103 |
0
| symbols.put("requiredMessage", buildRequiredMessage(field));
|
104 |
| |
105 |
0
| if (_minimumLength > 0)
|
106 |
0
| symbols.put("minimumLengthMessage", buildMinimumLengthMessage(field));
|
107 |
| |
108 |
0
| processValidatorScript(_scriptPath, cycle, field, symbols);
|
109 |
| } |
110 |
| |
111 |
| |
112 |
| |
113 |
| |
114 |
| |
115 |
0
| public String getScriptPath()
|
116 |
| { |
117 |
0
| return _scriptPath;
|
118 |
| } |
119 |
| |
120 |
| |
121 |
| |
122 |
| |
123 |
| |
124 |
| |
125 |
| |
126 |
| |
127 |
| |
128 |
| |
129 |
0
| public void setScriptPath(String scriptPath)
|
130 |
| { |
131 |
0
| _scriptPath = scriptPath;
|
132 |
| } |
133 |
| |
134 |
| |
135 |
0
| public String getMinimumLengthMessage()
|
136 |
| { |
137 |
0
| return _minimumLengthMessage;
|
138 |
| } |
139 |
| |
140 |
| |
141 |
| |
142 |
| |
143 |
| |
144 |
| |
145 |
| |
146 |
| |
147 |
1
| public void setMinimumLengthMessage(String string)
|
148 |
| { |
149 |
1
| _minimumLengthMessage = string;
|
150 |
| } |
151 |
| |
152 |
| |
153 |
| |
154 |
1
| protected String buildMinimumLengthMessage(IFormComponent field)
|
155 |
| { |
156 |
1
| String pattern = getPattern(_minimumLengthMessage, "field-too-short", field.getPage()
|
157 |
| .getLocale()); |
158 |
| |
159 |
1
| return formatString(pattern, Integer.toString(_minimumLength), field.getDisplayName());
|
160 |
| } |
161 |
| |
162 |
| } |