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 | 0 | { |
41 | 0 | } |
42 | |
|
43 | |
public Required(String initializer) |
44 | |
{ |
45 | 0 | super(initializer); |
46 | 0 | } |
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 | 0 | if (field.isDisabled()) |
57 | 0 | return; |
58 | |
|
59 | 0 | if ((object == null) |
60 | 0 | || (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 | 0 | String message = buildMessage(messages, field); |
65 | 0 | throw new ValidatorException(message, ValidationConstraint.REQUIRED); |
66 | |
} |
67 | 0 | } |
68 | |
|
69 | |
public String buildMessage(ValidationMessages messages, IFormComponent field) |
70 | |
{ |
71 | 0 | 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 | 0 | if(field.isDisabled()) |
82 | 0 | return; |
83 | |
|
84 | 0 | context.registerForFocus(ValidationConstants.REQUIRED_FIELD); |
85 | |
|
86 | 0 | JSONObject profile = context.getProfile(); |
87 | |
|
88 | 0 | accumulateProperty(profile, ValidationConstants.REQUIRED, field.getClientId()); |
89 | |
|
90 | 0 | accumulateProfileProperty(field, profile, |
91 | |
ValidationConstants.REQUIRED, buildMessage(context, field)); |
92 | 0 | } |
93 | |
|
94 | |
|
95 | |
|
96 | |
|
97 | |
public boolean isRequired() |
98 | |
{ |
99 | 0 | return true; |
100 | |
} |
101 | |
} |