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