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.json.JSONLiteral; |
23 |
|
import org.apache.tapestry.json.JSONObject; |
24 |
|
import org.apache.tapestry.util.RegexpMatcher; |
25 |
|
import org.apache.tapestry.valid.ValidationConstants; |
26 |
|
import org.apache.tapestry.valid.ValidationConstraint; |
27 |
|
import org.apache.tapestry.valid.ValidationStrings; |
28 |
|
import org.apache.tapestry.valid.ValidatorException; |
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
public class Pattern extends BaseValidator |
38 |
|
{ |
39 |
|
|
40 |
|
|
41 |
|
|
42 |
5 |
private RegexpMatcher _matcher = new RegexpMatcher(); |
43 |
|
|
44 |
|
private String _pattern; |
45 |
|
|
46 |
|
public Pattern() |
47 |
0 |
{ |
48 |
0 |
} |
49 |
|
|
50 |
|
public Pattern(String initializer) |
51 |
|
{ |
52 |
5 |
super(initializer); |
53 |
5 |
} |
54 |
|
|
55 |
|
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
56 |
|
throws ValidatorException |
57 |
|
{ |
58 |
3 |
String input = (String) object; |
59 |
|
|
60 |
3 |
if (!_matcher.matches(_pattern, input)) |
61 |
2 |
throw new ValidatorException(buildMessage(messages, field), |
62 |
|
ValidationConstraint.PATTERN_MISMATCH); |
63 |
1 |
} |
64 |
|
|
65 |
|
private String buildMessage(ValidationMessages messages, IFormComponent field) |
66 |
|
{ |
67 |
4 |
return messages.formatValidationMessage( |
68 |
|
getMessage(), |
69 |
|
ValidationStrings.PATTERN_MISMATCH, |
70 |
|
new Object[] |
71 |
|
{field.getDisplayName(), _pattern }); |
72 |
|
} |
73 |
|
|
74 |
|
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
75 |
|
FormComponentContributorContext context, IFormComponent field) |
76 |
|
{ |
77 |
2 |
String pattern = _matcher.getEscapedPatternString(_pattern); |
78 |
|
|
79 |
2 |
JSONObject profile = context.getProfile(); |
80 |
|
|
81 |
2 |
if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
82 |
2 |
profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
83 |
|
} |
84 |
2 |
JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
85 |
|
|
86 |
2 |
accumulateProperty(cons, field.getClientId(), |
87 |
|
new JSONLiteral("[tapestry.form.validation.isValidPattern,\"" |
88 |
|
+ pattern + "\"]")); |
89 |
|
|
90 |
2 |
accumulateProfileProperty(field, profile, |
91 |
|
ValidationConstants.CONSTRAINTS, buildMessage(context, field)); |
92 |
2 |
} |
93 |
|
|
94 |
|
public void setPattern(String pattern) |
95 |
|
{ |
96 |
5 |
_pattern = pattern; |
97 |
5 |
} |
98 |
|
|
99 |
|
public String getPattern() |
100 |
|
{ |
101 |
0 |
return _pattern; |
102 |
|
} |
103 |
|
} |