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 | 0 | { |
41 | 0 | } |
42 | |
|
43 | |
public MinLength(String initializer) |
44 | |
{ |
45 | 0 | super(initializer); |
46 | 0 | } |
47 | |
|
48 | |
public void setMinLength(int minLength) |
49 | |
{ |
50 | 0 | _minLength = minLength; |
51 | 0 | } |
52 | |
|
53 | |
public int getMinLength() |
54 | |
{ |
55 | 0 | return _minLength; |
56 | |
} |
57 | |
|
58 | |
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
59 | |
throws ValidatorException |
60 | |
{ |
61 | 0 | String string = (String) object; |
62 | |
|
63 | 0 | if (string.length() < _minLength) |
64 | 0 | throw new ValidatorException(buildMessage(messages, field), |
65 | |
ValidationConstraint.MINIMUM_WIDTH); |
66 | 0 | } |
67 | |
|
68 | |
protected String buildMessage(ValidationMessages messages, IFormComponent field) |
69 | |
{ |
70 | 0 | 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 | 0 | JSONObject profile = context.getProfile(); |
81 | |
|
82 | 0 | if (!profile.has(ValidationConstants.CONSTRAINTS)) { |
83 | 0 | profile.put(ValidationConstants.CONSTRAINTS, new JSONObject()); |
84 | |
} |
85 | 0 | JSONObject cons = profile.getJSONObject(ValidationConstants.CONSTRAINTS); |
86 | |
|
87 | 0 | accumulateProperty(cons, field.getClientId(), |
88 | |
new JSONLiteral("[tapestry.form.validation.isText,{" |
89 | |
+ "minlength:" + _minLength + "}]")); |
90 | |
|
91 | 0 | accumulateProfileProperty(field, profile, |
92 | |
ValidationConstants.CONSTRAINTS, buildMessage(context, field)); |
93 | 0 | } |
94 | |
} |