View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.renderkit.html.ext;
20  
21  import java.io.IOException;
22  
23  import javax.faces.component.UIComponent;
24  import javax.faces.context.FacesContext;
25  import javax.faces.context.ResponseWriter;
26  
27  import org.apache.myfaces.custom.clientvalidation.common.CVUtils;
28  import org.apache.myfaces.custom.util.ComponentUtils;
29  import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlFormRendererBase;
30  
31  /**
32   * 
33   * @JSFRenderer
34   *   renderKitId = "HTML_BASIC" 
35   *   family = "javax.faces.Form"
36   *   type = "javax.faces.Form"
37   * 
38   * @author cagatay (latest modification by $Author: skitching $)
39   * @version $Revision: 673833 $ $Date: 2008-07-03 16:58:05 -0500 (Thu, 03 Jul 2008) $
40   */
41  public class HtmlFormRenderer extends HtmlFormRendererBase{
42  
43      private static String CLIENT_VALIDATON_SCRIPT = "return tomahawk.executeClientLifeCycle();";
44      
45      public void encodeBegin(FacesContext facesContext, UIComponent component) throws IOException {
46          if(CVUtils.isCVEnabled()) {
47              ComponentUtils.decorateEventAttribute(component, "onsubmit", CLIENT_VALIDATON_SCRIPT);
48          }
49          
50          super.encodeBegin(facesContext, component);
51      }
52      
53      public void encodeEnd(FacesContext facesContext, UIComponent component) throws IOException {
54          if(CVUtils.isCVEnabled()) {
55              encodeBypassCVField(facesContext, component);
56          }
57          
58          super.encodeEnd(facesContext, component);
59          
60          if(CVUtils.isCVEnabled()) {
61              CVUtils.encodeJavascript(facesContext);
62              CVUtils.queueCVCalls(facesContext.getViewRoot());
63              CVUtils.encodeValidationScript(facesContext);
64          }
65      }
66  
67      //Renders a hidden input field that will act as a flag to bypass client validation or not
68      private void encodeBypassCVField(FacesContext facesContext, UIComponent component) throws IOException{
69          ResponseWriter writer = facesContext.getResponseWriter();
70          
71          writer.startElement("input", component);
72          writer.writeAttribute("type", "hidden", null);
73          writer.writeAttribute("id", CVUtils.BYPASS_CLIENT_VALIDATION_FIELD, null);
74          writer.writeAttribute("name", CVUtils.BYPASS_CLIENT_VALIDATION_FIELD, null);
75          writer.writeAttribute("value", "false", null);                //immediate command components make this value true when they're clicked 
76          writer.endElement("input");
77      }
78  }