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.util.RegexpMatcher; |
24 |
| import org.apache.tapestry.valid.ValidationConstraint; |
25 |
| import org.apache.tapestry.valid.ValidationStrings; |
26 |
| import org.apache.tapestry.valid.ValidatorException; |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| public class Pattern extends BaseValidator |
36 |
| { |
37 |
| |
38 |
| |
39 |
| |
40 |
| private RegexpMatcher _matcher = new RegexpMatcher(); |
41 |
| |
42 |
| private String _pattern; |
43 |
| |
44 |
0
| public Pattern()
|
45 |
| { |
46 |
| } |
47 |
| |
48 |
10
| public Pattern(String initializer)
|
49 |
| { |
50 |
10
| super(initializer);
|
51 |
| } |
52 |
| |
53 |
6
| public void validate(IFormComponent field, ValidationMessages messages, Object object)
|
54 |
| throws ValidatorException |
55 |
| { |
56 |
6
| String input = (String) object;
|
57 |
| |
58 |
6
| if (!_matcher.matches(_pattern, input))
|
59 |
4
| throw new ValidatorException(buildMessage(messages, field),
|
60 |
| ValidationConstraint.PATTERN_MISMATCH); |
61 |
| } |
62 |
| |
63 |
8
| private String buildMessage(ValidationMessages messages, IFormComponent field)
|
64 |
| { |
65 |
8
| return messages.formatValidationMessage(
|
66 |
| getMessage(), |
67 |
| ValidationStrings.REGEX_MISMATCH, |
68 |
| new Object[] |
69 |
| { field.getDisplayName() }); |
70 |
| } |
71 |
| |
72 |
4
| public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
|
73 |
| FormComponentContributorContext context, IFormComponent field) |
74 |
| { |
75 |
4
| context.includeClasspathScript("/org/apache/tapestry/form/validator/RegExValidator.js");
|
76 |
| |
77 |
4
| String pattern = _matcher.getEscapedPatternString(_pattern);
|
78 |
4
| String message = buildMessage(context, field);
|
79 |
| |
80 |
4
| StringBuffer buffer = new StringBuffer("function(event) { Tapestry.validate_regex(event, '");
|
81 |
4
| buffer.append(field.getClientId());
|
82 |
4
| buffer.append("', '");
|
83 |
4
| buffer.append(pattern);
|
84 |
4
| buffer.append("', ");
|
85 |
4
| buffer.append(TapestryUtils.enquote(message));
|
86 |
4
| buffer.append("); }");
|
87 |
| |
88 |
4
| context.addSubmitHandler(buffer.toString());
|
89 |
| } |
90 |
| |
91 |
10
| public void setPattern(String pattern)
|
92 |
| { |
93 |
10
| _pattern = pattern;
|
94 |
| } |
95 |
| |
96 |
| } |