1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
package org.apache.tapestry.form.validator; |
16 |
|
|
17 |
|
import java.util.Collection; |
18 |
|
|
19 |
|
import org.apache.tapestry.IMarkupWriter; |
20 |
|
import org.apache.tapestry.IRequestCycle; |
21 |
|
import org.apache.tapestry.form.FormComponentContributorContext; |
22 |
|
import org.apache.tapestry.form.IFormComponent; |
23 |
|
import org.apache.tapestry.form.ValidationMessages; |
24 |
|
import org.apache.tapestry.json.JSONObject; |
25 |
|
import org.apache.tapestry.multipart.UploadPart; |
26 |
|
import org.apache.tapestry.valid.ValidationConstants; |
27 |
|
import org.apache.tapestry.valid.ValidationConstraint; |
28 |
|
import org.apache.tapestry.valid.ValidationStrings; |
29 |
|
import org.apache.tapestry.valid.ValidatorException; |
30 |
|
|
31 |
|
|
32 |
|
|
33 |
|
|
34 |
|
|
35 |
|
|
36 |
|
|
37 |
|
public class Required extends BaseValidator |
38 |
|
{ |
39 |
|
public Required() |
40 |
10 |
{ |
41 |
10 |
} |
42 |
|
|
43 |
|
public Required(String initializer) |
44 |
|
{ |
45 |
1 |
super(initializer); |
46 |
1 |
} |
47 |
|
|
48 |
|
public boolean getAcceptsNull() |
49 |
|
{ |
50 |
0 |
return true; |
51 |
|
} |
52 |
|
|
53 |
|
public void validate(IFormComponent field, ValidationMessages messages, Object object) |
54 |
|
throws ValidatorException |
55 |
|
{ |
56 |
6 |
if (field.isDisabled()) |
57 |
1 |
return; |
58 |
|
|
59 |
5 |
if ((object == null) |
60 |
3 |
|| (String.class.isInstance(object) && (((String) object).length() == 0)) |
61 |
|
|| (Collection.class.isInstance(object) && ((Collection) object).isEmpty()) |
62 |
|
|| (UploadPart.class.isInstance(object) && ((UploadPart) object).getSize() < 1)) |
63 |
|
{ |
64 |
4 |
String message = buildMessage(messages, field); |
65 |
4 |
throw new ValidatorException(message, ValidationConstraint.REQUIRED); |
66 |
|
} |
67 |
1 |
} |
68 |
|
|
69 |
|
public String buildMessage(ValidationMessages messages, IFormComponent field) |
70 |
|
{ |
71 |
5 |
return messages.formatValidationMessage( |
72 |
|
getMessage(), |
73 |
|
ValidationStrings.REQUIRED_FIELD, |
74 |
|
new Object[] |
75 |
|
{ field.getDisplayName() }); |
76 |
|
} |
77 |
|
|
78 |
|
public void renderContribution(IMarkupWriter writer, IRequestCycle cycle, |
79 |
|
FormComponentContributorContext context, IFormComponent field) |
80 |
|
{ |
81 |
2 |
if(field.isDisabled()) |
82 |
1 |
return; |
83 |
|
|
84 |
1 |
context.registerForFocus(ValidationConstants.REQUIRED_FIELD); |
85 |
|
|
86 |
1 |
JSONObject profile = context.getProfile(); |
87 |
|
|
88 |
1 |
accumulateProperty(profile, ValidationConstants.REQUIRED, field.getClientId()); |
89 |
|
|
90 |
1 |
accumulateProfileProperty(field, profile, |
91 |
|
ValidationConstants.REQUIRED, buildMessage(context, field)); |
92 |
1 |
} |
93 |
|
|
94 |
|
|
95 |
|
|
96 |
|
|
97 |
|
public boolean isRequired() |
98 |
|
{ |
99 |
2 |
return true; |
100 |
|
} |
101 |
|
} |