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.TapestryUtils; |
22 |
| import org.apache.tapestry.form.FormComponentContributorContext; |
23 |
| import org.apache.tapestry.form.IFormComponent; |
24 |
| import org.apache.tapestry.form.ValidationMessages; |
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 |
7
| public Required()
|
40 |
| { |
41 |
| } |
42 |
| |
43 |
1
| public Required(String initializer)
|
44 |
| { |
45 |
1
| super(initializer);
|
46 |
| } |
47 |
| |
48 |
0
| public boolean getAcceptsNull()
|
49 |
| { |
50 |
0
| return true;
|
51 |
| } |
52 |
| |
53 |
5
| public void validate(IFormComponent field, ValidationMessages messages, Object object)
|
54 |
| throws ValidatorException |
55 |
| { |
56 |
5
| if ((object == null)
|
57 |
| || (String.class.isInstance(object) && (((String) object).length() == 0)) |
58 |
| || (Collection.class.isInstance(object) && ((Collection) object).isEmpty()) |
59 |
| || (UploadPart.class.isInstance(object) && ((UploadPart) object).getSize() < 1)) |
60 |
| { |
61 |
4
| String message = buildMessage(messages, field);
|
62 |
4
| throw new ValidatorException(message, ValidationConstraint.REQUIRED);
|
63 |
| } |
64 |
| } |
65 |
| |
66 |
5
| private String buildMessage(ValidationMessages messages, IFormComponent field)
|
67 |
| { |
68 |
5
| return messages.formatValidationMessage(
|
69 |
| getMessage(), |
70 |
| ValidationStrings.REQUIRED_FIELD, |
71 |
| new Object[] |
72 |
| { field.getDisplayName() }); |
73 |
| } |
74 |
| |
75 |
1
| public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
|
76 |
| FormComponentContributorContext context, IFormComponent field) |
77 |
| { |
78 |
1
| context.registerForFocus(ValidationConstants.REQUIRED_FIELD);
|
79 |
| |
80 |
1
| StringBuffer buffer = new StringBuffer("function(event) { Tapestry.require_field(event, '");
|
81 |
1
| buffer.append(field.getClientId());
|
82 |
1
| buffer.append("', ");
|
83 |
1
| buffer.append(TapestryUtils.enquote(buildMessage(context, field)));
|
84 |
1
| buffer.append("); }");
|
85 |
| |
86 |
1
| context.addSubmitHandler(buffer.toString());
|
87 |
| } |
88 |
| |
89 |
| |
90 |
| |
91 |
| |
92 |
1
| public boolean isRequired()
|
93 |
| { |
94 |
1
| return true;
|
95 |
| } |
96 |
| } |