KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > form > RequirableFieldSupportImpl


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 JavaDoc;
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     public void render(RequirableField component, IMarkupWriter writer, IRequestCycle cycle)
39     {
40         IForm form = component.getForm();
41         
42         if (component.isRequired() && form.isClientValidationEnabled())
43         {
44             String JavaDoc function = "require(event, document." + form.getName() + "." + component.getName() + ",'" + buildRequiredMessage(component) + "')";
45             
46             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     public void rewind(RequirableField component, IMarkupWriter writer, IRequestCycle cycle)
54     {
55         try
56         {
57             String JavaDoc value = component.getSubmittedValue(cycle);
58             
59             if (component.isRequired() && HiveMind.isBlank(value))
60             {
61                 throw new ValidatorException(buildRequiredMessage(component), ValidationConstraint.REQUIRED);
62             }
63             
64             component.bind(writer, cycle);
65         }
66         catch (ValidatorException e)
67         {
68             component.getForm().getDelegate().record(e);
69         }
70     }
71     
72     protected String JavaDoc buildRequiredMessage(RequirableField component)
73     {
74         return MessageFormat.format(component.getRequiredMessage(), new Object JavaDoc[] { component.getDisplayName() });
75     }
76 }
77
Popular Tags