1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.form.validator; |
16 |
| |
17 |
| import org.apache.hivemind.util.PropertyUtils; |
18 |
| import org.apache.tapestry.IMarkupWriter; |
19 |
| import org.apache.tapestry.IRequestCycle; |
20 |
| import org.apache.tapestry.form.FormComponentContributorContext; |
21 |
| import org.apache.tapestry.form.IFormComponent; |
22 |
| import org.apache.tapestry.form.ValidationMessages; |
23 |
| import org.apache.tapestry.valid.ValidationConstraint; |
24 |
| import org.apache.tapestry.valid.ValidationStrings; |
25 |
| import org.apache.tapestry.valid.ValidatorException; |
26 |
| |
27 |
| |
28 |
| |
29 |
| |
30 |
| |
31 |
| |
32 |
| |
33 |
| public class Required extends BaseValidator |
34 |
| { |
35 |
4
| public Required()
|
36 |
| { |
37 |
| } |
38 |
| |
39 |
1
| public Required(String initializer)
|
40 |
| { |
41 |
1
| super(initializer);
|
42 |
| } |
43 |
| |
44 |
0
| public boolean getAcceptsNull()
|
45 |
| { |
46 |
0
| return true;
|
47 |
| } |
48 |
| |
49 |
3
| public void validate(IFormComponent field, ValidationMessages messages, Object object)
|
50 |
| throws ValidatorException |
51 |
| { |
52 |
3
| if (object == null)
|
53 |
| { |
54 |
2
| String message = buildMessage(messages, field);
|
55 |
2
| throw new ValidatorException(message, ValidationConstraint.REQUIRED);
|
56 |
| } |
57 |
| } |
58 |
| |
59 |
3
| private String buildMessage(ValidationMessages messages, IFormComponent field)
|
60 |
| { |
61 |
3
| return messages.formatValidationMessage(
|
62 |
| getMessage(), |
63 |
| ValidationStrings.REQUIRED_TEXT_FIELD, |
64 |
| new Object[] |
65 |
| { field.getDisplayName() }); |
66 |
| } |
67 |
| |
68 |
1
| public void renderContribution(IMarkupWriter writer, IRequestCycle cycle,
|
69 |
| FormComponentContributorContext context, IFormComponent field) |
70 |
| { |
71 |
1
| StringBuffer buffer = new StringBuffer("function(event) { require(event, ");
|
72 |
1
| buffer.append(context.getFieldDOM());
|
73 |
1
| buffer.append(", '");
|
74 |
1
| buffer.append(buildMessage(context, field));
|
75 |
1
| buffer.append("'); }");
|
76 |
| |
77 |
1
| context.addSubmitListener(buffer.toString());
|
78 |
| } |
79 |
| |
80 |
| } |