|
|||||||||||||||||||
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 | |||||||||||||||
RequirableFieldSupportImpl.java | 100% | 100% | 100% | 100% |
|
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; | |
16 | ||
17 | import java.text.MessageFormat; | |
18 | ||
19 | import org.apache.hivemind.HiveMind; | |
20 | import org.apache.tapestry.IForm; | |
21 | import org.apache.tapestry.IMarkupWriter; | |
22 | import org.apache.tapestry.IRequestCycle; | |
23 | import org.apache.tapestry.valid.ValidationConstraint; | |
24 | import org.apache.tapestry.valid.ValidatorException; | |
25 | ||
26 | /** | |
27 | * Default {@link RequirableFieldSupport} implementation. This implementation generates | |
28 | * calls to a static javascript function during render if client-side validation is enabled. | |
29 | * | |
30 | * @author Paul Ferraro | |
31 | * @since 4.0 | |
32 | */ | |
33 | public class RequirableFieldSupportImpl implements RequirableFieldSupport | |
34 | { | |
35 | /** | |
36 | * @see org.apache.tapestry.form.RequirableFieldSupport#render(org.apache.tapestry.form.RequirableField, org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle) | |
37 | */ | |
38 | 29 | public void render(RequirableField component, IMarkupWriter writer, IRequestCycle cycle) |
39 | { | |
40 | 29 | IForm form = component.getForm(); |
41 | ||
42 | 29 | if (component.isRequired() && form.isClientValidationEnabled()) |
43 | { | |
44 | 1 | String function = "require(event, document." + form.getName() + "." + component.getName() + ",'" + buildRequiredMessage(component) + "')"; |
45 | ||
46 | 1 | form.addEventHandler(FormEventType.SUBMIT, function); |
47 | } | |
48 | } | |
49 | ||
50 | /** | |
51 | * @see org.apache.tapestry.form.RequirableFieldSupport#rewind(org.apache.tapestry.form.RequirableField, org.apache.tapestry.IMarkupWriter, org.apache.tapestry.IRequestCycle) | |
52 | */ | |
53 | 19 | public void rewind(RequirableField component, IMarkupWriter writer, IRequestCycle cycle) |
54 | { | |
55 | 19 | try |
56 | { | |
57 | 19 | String value = component.getSubmittedValue(cycle); |
58 | ||
59 | 19 | if (component.isRequired() && HiveMind.isBlank(value)) |
60 | { | |
61 | 1 | throw new ValidatorException(buildRequiredMessage(component), ValidationConstraint.REQUIRED); |
62 | } | |
63 | ||
64 | 18 | component.bind(writer, cycle); |
65 | } | |
66 | catch (ValidatorException e) | |
67 | { | |
68 | 1 | component.getForm().getDelegate().record(e); |
69 | } | |
70 | } | |
71 | ||
72 | 2 | protected String buildRequiredMessage(RequirableField component) |
73 | { | |
74 | 2 | return MessageFormat.format(component.getRequiredMessage(), new Object[] { component.getDisplayName() }); |
75 | } | |
76 | } |
|