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