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 | |
|
33 | |
public class StringValidator extends BaseValidator |
34 | |
{ |
35 | |
|
36 | |
private int _minimumLength; |
37 | |
|
38 | |
private String _minimumLengthMessage; |
39 | |
|
40 | |
|
41 | |
|
42 | 0 | private String _scriptPath = "/org/apache/tapestry/valid/StringValidator.script"; |
43 | |
|
44 | |
public StringValidator() |
45 | 0 | { |
46 | 0 | } |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
|
54 | |
|
55 | |
public StringValidator(String initializer) |
56 | |
{ |
57 | 0 | super(initializer); |
58 | 0 | } |
59 | |
|
60 | |
public String toString(IFormComponent field, Object value) |
61 | |
{ |
62 | 0 | if (value == null) return null; |
63 | |
|
64 | 0 | return value.toString(); |
65 | |
} |
66 | |
|
67 | |
public Object toObject(IFormComponent field, String input) |
68 | |
throws ValidatorException |
69 | |
{ |
70 | 0 | if (checkRequired(field, input)) return null; |
71 | |
|
72 | 0 | if (_minimumLength > 0 && input.length() < _minimumLength) |
73 | 0 | throw new ValidatorException(buildMinimumLengthMessage(field), |
74 | |
ValidationConstraint.MINIMUM_WIDTH); |
75 | |
|
76 | 0 | return input; |
77 | |
} |
78 | |
|
79 | |
public int getMinimumLength() |
80 | |
{ |
81 | 0 | return _minimumLength; |
82 | |
} |
83 | |
|
84 | |
public void setMinimumLength(int minimumLength) |
85 | |
{ |
86 | 0 | _minimumLength = minimumLength; |
87 | 0 | } |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
|
93 | |
public void renderValidatorContribution(IFormComponent field, |
94 | |
IMarkupWriter writer, IRequestCycle cycle) |
95 | |
{ |
96 | 0 | if (!isClientScriptingEnabled()) return; |
97 | |
|
98 | 0 | if (!(isRequired() || _minimumLength > 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", |
107 | |
buildMinimumLengthMessage(field)); |
108 | |
|
109 | 0 | processValidatorScript(_scriptPath, cycle, field, symbols); |
110 | 0 | } |
111 | |
|
112 | |
|
113 | |
|
114 | |
|
115 | |
|
116 | |
public String getScriptPath() |
117 | |
{ |
118 | 0 | return _scriptPath; |
119 | |
} |
120 | |
|
121 | |
|
122 | |
|
123 | |
|
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
public void setScriptPath(String scriptPath) |
132 | |
{ |
133 | 0 | _scriptPath = scriptPath; |
134 | 0 | } |
135 | |
|
136 | |
|
137 | |
public String getMinimumLengthMessage() |
138 | |
{ |
139 | 0 | return _minimumLengthMessage; |
140 | |
} |
141 | |
|
142 | |
|
143 | |
|
144 | |
|
145 | |
|
146 | |
|
147 | |
|
148 | |
|
149 | |
public void setMinimumLengthMessage(String string) |
150 | |
{ |
151 | 0 | _minimumLengthMessage = string; |
152 | 0 | } |
153 | |
|
154 | |
|
155 | |
|
156 | |
protected String buildMinimumLengthMessage(IFormComponent field) |
157 | |
{ |
158 | 0 | String pattern = getPattern(_minimumLengthMessage, "field-too-short", |
159 | |
field.getPage().getLocale()); |
160 | |
|
161 | 0 | return formatString(pattern, Integer.toString(_minimumLength), field |
162 | |
.getDisplayName()); |
163 | |
} |
164 | |
|
165 | |
} |