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 Email extends BaseValidator |
38 |
|
{ |
39 |
|
public static final String PATTERN = "^[A-Za-z0-9]+([-_\\.]*[A-Za-z0-9]+)*@[A-Za-z0-9]+([-_\\.]*[A-Za-z0-9]+)*(\\.[_A-Za-z]{2,6})$"; |
40 |
|
|
41 |
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
7 |
private RegexpMatcher _matcher = new RegexpMatcher(); |
46 |
|
|
47 |
|
public Email() |
48 |
5 |
{ |
49 |
5 |
} |
50 |
|
|
51 |
|
public Email(String initializer) |
52 |
|
{ |
53 |
2 |
super(initializer); |
54 |
2 |
} |
55 |
|
|
56 |
|
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
57 |
|
throws ValidatorException |
58 |
|
{ |
59 |
5 |
String input = (String) object; |
60 |
|
|
61 |
5 |
if (!_matcher.matches(PATTERN, input)) |
62 |
2 |
throw new ValidatorException(buildMessage(messages, field), |
63 |
|
ValidationConstraint.EMAIL_FORMAT); |
64 |
3 |
} |
65 |
|
|
66 |
|
private String buildMessage(ValidationMessages messages, IFormComponent field) |
67 |
|
{ |
68 |
4 |
return messages.formatValidationMessage( |
69 |
|
getMessage(), |
70 |
|
ValidationStrings.INVALID_EMAIL, |
71 |
|
new Object[] |
72 |
|
{ field.getDisplayName() }); |
73 |
|
} |
74 |
|
|
75 |
|
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
76 |
|
FormComponentContributorContext context, IFormComponent field) |
77 |
|
{ |
78 |
2 |
context.addInitializationScript(field, "dojo.require(\"dojo.validate.web\");"); |
79 |
|
|
80 |
2 |
JSONObject profile = context.getProfile(); |
81 |
|
|
82 |
2 |
if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
83 |
2 |
profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
84 |
|
} |
85 |
2 |
JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
86 |
|
|
87 |
2 |
accumulateProperty(cons, field.getClientId(), |
88 |
|
new JSONLiteral("[dojo.validate.isEmailAddress,false,true]")); |
89 |
|
|
90 |
2 |
accumulateProfileProperty(field, profile, |
91 |
|
ValidationConstants.CONSTRAINTS, buildMessage(context, field)); |
92 |
2 |
} |
93 |
|
} |