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