|
|||||||||||||||||||
30 day Evaluation License registered to hlship@comcast.net Your 30 day evaluation period has expired. Please visit http://www.cenqua.com to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Required.java | 100% | 91.7% | 83.3% | 90% |
|
1 | // Copyright 2005 The Apache Software Foundation | |
2 | // | |
3 | // Licensed under the Apache License, Version 2.0 (the "License"); | |
4 | // you may not use this file except in compliance with the License. | |
5 | // You may obtain a copy of the License at | |
6 | // | |
7 | // http://www.apache.org/licenses/LICENSE-2.0 | |
8 | // | |
9 | // Unless required by applicable law or agreed to in writing, software | |
10 | // distributed under the License is distributed on an "AS IS" BASIS, | |
11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
12 | // See the License for the specific language governing permissions and | |
13 | // limitations under the License. | |
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 | * {@link org.apache.tapestry.form.validator.Validator} that ensures a value was supplied. | |
29 | * | |
30 | * @author Howard Lewis Ship | |
31 | * @since 4.0 | |
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 | } |
|