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.valid.ValidationConstants; |
25 |
|
import org.apache.tapestry.valid.ValidationConstraint; |
26 |
|
import org.apache.tapestry.valid.ValidationStrings; |
27 |
|
import org.apache.tapestry.valid.ValidatorException; |
28 |
|
|
29 |
|
|
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
public class MinLength extends BaseValidator |
36 |
|
{ |
37 |
|
private int _minLength; |
38 |
|
|
39 |
|
public MinLength() |
40 |
2 |
{ |
41 |
2 |
} |
42 |
|
|
43 |
|
public MinLength(String initializer) |
44 |
|
{ |
45 |
5 |
super(initializer); |
46 |
5 |
} |
47 |
|
|
48 |
|
public void setMinLength(int minLength) |
49 |
|
{ |
50 |
6 |
_minLength = minLength; |
51 |
6 |
} |
52 |
|
|
53 |
|
public int getMinLength() |
54 |
|
{ |
55 |
1 |
return _minLength; |
56 |
|
} |
57 |
|
|
58 |
|
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
59 |
|
throws ValidatorException |
60 |
|
{ |
61 |
3 |
String string = (String) object; |
62 |
|
|
63 |
3 |
if (string.length() < _minLength) |
64 |
2 |
throw new ValidatorException(buildMessage(messages, field), |
65 |
|
ValidationConstraint.MINIMUM_WIDTH); |
66 |
1 |
} |
67 |
|
|
68 |
|
protected String buildMessage(ValidationMessages messages, IFormComponent field) |
69 |
|
{ |
70 |
4 |
return messages.formatValidationMessage( |
71 |
|
getMessage(), |
72 |
|
ValidationStrings.VALUE_TOO_SHORT, |
73 |
|
new Object[] |
74 |
|
{ new Integer(_minLength), field.getDisplayName() }); |
75 |
|
} |
76 |
|
|
77 |
|
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
78 |
|
FormComponentContributorContext context, IFormComponent field) |
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.isText,{" |
89 |
|
+ "minlength:" + _minLength + "}]")); |
90 |
|
|
91 |
2 |
accumulateProfileProperty(field, profile, |
92 |
|
ValidationConstants.CONSTRAINTS, buildMessage(context, field)); |
93 |
2 |
} |
94 |
|
} |