Class ValidatorTool


  • @DefaultKey("validator")
    @ValidScope("request")
    public class ValidatorTool
    extends Object

    View tool that works with Struts Validator to produce client side javascript validation for your forms.

    Usage:

     Template example:
    
     $validator.getJavascript("nameOfYourForm")
    
     Toolbox configuration:
     <tools>
       <toolbox scope="request">
         <tool class="org.apache.velocity.tools.struts.ValidatorTool"/>
       </toolbox>
     </tools>
     

    This is an adaptation of the JavascriptValidatorTag from the Struts 1.1 validator library.

    Since:
    VelocityTools 1.1
    Version:
    $Revision$ $Date$
    Author:
    David Winterfeldt, David Graham, Marino A. Jonsson, Nathan Bubna
    • Field Summary

      Fields 
      Modifier and Type Field Description
      protected javax.servlet.ServletContext app
      A reference to the ServletContext
      protected ViewContext context
      A reference to the ViewContext
      protected String jsFormName
      formName is used for both Javascript and non-javascript validations.
      protected javax.servlet.http.HttpServletRequest request
      A reference to the HttpServletRequest.
      protected org.apache.commons.validator.ValidatorResources resources
      A reference to the ValidatorResources.
      protected javax.servlet.http.HttpSession session
      A reference to the HttpSession.
    • Constructor Summary

      Constructors 
      Constructor Description
      ValidatorTool()  
    • Method Summary

      All Methods Instance Methods Concrete Methods Deprecated Methods 
      Modifier and Type Method Description
      void configure​(Map<String,​Object> params)
      Initializes this tool.
      protected List<org.apache.commons.validator.ValidatorAction> createActionList​(org.apache.commons.validator.ValidatorResources resources, org.apache.commons.validator.Form form)
      Get List of actions for the given Form.
      protected String createMethods​(List<org.apache.commons.validator.ValidatorAction> actions)
      Creates the JavaScript methods list from the given actions.
      protected String escapeJavascript​(String str)
      Backslash-escapes the following characters from the input string: ", ', \, \r, \n.
      boolean getCdata()
      Returns the cdata setting "true" or "false".
      String getDynamicJavascript()
      Render just the dynamic JavaScript to perform validations based on the form name attribute of the action mapping associated with the current request (if such exists).
      String getDynamicJavascript​(String formName)
      Render just the dynamic JavaScript to perform validations based on the supplied form name.
      protected String getDynamicJavascript​(org.apache.commons.validator.ValidatorResources resources, Locale locale, org.apache.commons.validator.Form form)
      Generates the dynamic JavaScript for the form.
      boolean getHtmlComment()
      Gets whether or not to delimit the JavaScript with html comments.
      String getJavascript()
      Render both dynamic and static JavaScript to perform validations based on the form name attribute of the action mapping associated with the current request (if such exists).
      String getJavascript​(String formName)
      Render both dynamic and static JavaScript to perform validations based on the supplied form name.
      protected String getJavascript​(String formName, boolean getStatic)
      Render both dynamic and static JavaScript to perform validations based on the supplied form name.
      protected String getJavascriptBegin​(String methods)
      Returns the opening script element and some initial javascript.
      protected String getJavascriptEnd()
      Returns the closing script element.
      protected String getJavascriptStaticMethods​(org.apache.commons.validator.ValidatorResources resources)  
      String getMethod()
      Gets the method name that will be used for the Javascript validation method name if it has a value.
      int getPage()
      Gets the current page number of a multi-part form.
      String getSrc()
      Gets the src attribute's value when defining the html script element.
      String getStaticJavascript()
      Render just the static JavaScript methods.
      boolean getXhtml()
      Returns the XHTML setting.
      boolean getXMLMode()
      Returns the XMLMode setting.
      void init​(Object obj)
      Deprecated.
      void setCdata​(boolean cdata)
      Sets the cdata status.
      void setHtmlComment​(boolean htmlComment)
      Sets whether or not to delimit the JavaScript with html comments.
      void setMethod​(String methodName)
      Sets the method name that will be used for the Javascript validation method name if it has a value.
      void setPage​(int page)
      Sets the current page number of a multi-part form.
      void setSrc​(String src)
      Sets the src attribute's value (used to include an external script resource) when defining the html script element.
      void setXhtml​(boolean xhtml)
      Sets the XML mode status.
      void setXMLMode​(boolean xmlMode)
      Sets the XML mode status.
    • Field Detail

      • context

        protected ViewContext context
        A reference to the ViewContext
      • app

        protected javax.servlet.ServletContext app
        A reference to the ServletContext
      • request

        protected javax.servlet.http.HttpServletRequest request
        A reference to the HttpServletRequest.
      • session

        protected javax.servlet.http.HttpSession session
        A reference to the HttpSession.
      • resources

        protected org.apache.commons.validator.ValidatorResources resources
        A reference to the ValidatorResources.
      • jsFormName

        protected String jsFormName
        formName is used for both Javascript and non-javascript validations. For the javascript validations, there is the possibility that we will be rewriting the formName (if it is a ValidatorActionForm instead of just a ValidatorForm) so we need another variable to hold the formName just for javascript usage.
    • Constructor Detail

      • ValidatorTool

        public ValidatorTool()
    • Method Detail

      • configure

        public void configure​(Map<String,​Object> params)
        Initializes this tool.
        Parameters:
        params - the Map of configuration parameters
        Throws:
        IllegalArgumentException - if the param is not a ViewContext
      • getPage

        public int getPage()
        Gets the current page number of a multi-part form. Only field validations with a matching page number will be generated that match the current page number. Only valid when the formName attribute is set.
        Returns:
        the current page number of a multi-part form
      • setPage

        public void setPage​(int page)
        Sets the current page number of a multi-part form. Only field validations with a matching page number will be generated that match the current page number.
        Parameters:
        page - the current page number of a multi-part form
      • getMethod

        public String getMethod()
        Gets the method name that will be used for the Javascript validation method name if it has a value. This overrides the auto-generated method name based on the key (form name) passed in.
        Returns:
        the method name that will be used for the Javascript validation method
      • setMethod

        public void setMethod​(String methodName)
        Sets the method name that will be used for the Javascript validation method name if it has a value. This overrides the auto-generated method name based on the key (form name) passed in.
        Parameters:
        methodName - the method name that will be used for the Javascript validation method name
      • getHtmlComment

        public boolean getHtmlComment()
        Gets whether or not to delimit the JavaScript with html comments. If this is set to 'true', which is the default, html comments will surround the JavaScript.
        Returns:
        true if the JavaScript should be delimited with html comments
      • setHtmlComment

        public void setHtmlComment​(boolean htmlComment)
        Sets whether or not to delimit the JavaScript with html comments. If this is set to 'true', which is the default, html comments will surround the JavaScript.
        Parameters:
        htmlComment - whether or not to delimit the JavaScript with html comments
      • getSrc

        public String getSrc()
        Gets the src attribute's value when defining the html script element.
        Returns:
        the src attribute's value
      • setSrc

        public void setSrc​(String src)
        Sets the src attribute's value (used to include an external script resource) when defining the html script element. The src attribute is only recognized when the formName attribute is specified.
        Parameters:
        src - the src attribute's value
      • getCdata

        public boolean getCdata()
        Returns the cdata setting "true" or "false".
        Returns:
        boolean - "true" if JavaScript will be hidden in a CDATA section
      • setCdata

        public void setCdata​(boolean cdata)
        Sets the cdata status.
        Parameters:
        cdata - The cdata to set
      • getXhtml

        public boolean getXhtml()
        Returns the XHTML setting.
        Returns:
        boolean - "true" if JavaScript will use &amp; operators and suppress HTML comment wrappers.
      • setXhtml

        public void setXhtml​(boolean xhtml)
        Sets the XML mode status.
        Parameters:
        xhtml - The XHTML mode to set
      • getXMLMode

        public boolean getXMLMode()
        Returns the XMLMode setting.
        Returns:
        boolean - "true" if JavaScript will use &amp; operators and suppress HTML comment wrappers.
      • setXMLMode

        public void setXMLMode​(boolean xmlMode)
        Sets the XML mode status. If true, also sets XHTML to true. Setting this to true will force the use of &amp; for logical AND operations and will alwasy suppress the use of HTML comments around Javascript code.
        Parameters:
        xmlMode - The XML mode to set
      • getJavascript

        public String getJavascript()
                             throws Exception
        Render both dynamic and static JavaScript to perform validations based on the form name attribute of the action mapping associated with the current request (if such exists).
        Returns:
        the javascript for the current form
        Throws:
        Exception
      • getJavascript

        public String getJavascript​(String formName)
                             throws Exception
        Render both dynamic and static JavaScript to perform validations based on the supplied form name.
        Parameters:
        formName - the key (form name)
        Returns:
        the Javascript for the specified form
        Throws:
        Exception
      • getDynamicJavascript

        public String getDynamicJavascript()
                                    throws Exception
        Render just the dynamic JavaScript to perform validations based on the form name attribute of the action mapping associated with the current request (if such exists). Useful i.e. if the static parts are located in a seperate .js file.
        Returns:
        the javascript for the current form
        Throws:
        Exception
      • getStaticJavascript

        public String getStaticJavascript()
                                   throws Exception
        Render just the static JavaScript methods. Useful i.e. if the static parts should be located in a seperate .js file.
        Returns:
        all static Javascript methods
        Throws:
        Exception
      • getDynamicJavascript

        public String getDynamicJavascript​(String formName)
                                    throws Exception
        Render just the dynamic JavaScript to perform validations based on the supplied form name. Useful i.e. if the static parts are located in a separate .js file.
        Parameters:
        formName - the key (form name)
        Returns:
        the dynamic Javascript for the specified form
        Throws:
        Exception
      • getJavascript

        protected String getJavascript​(String formName,
                                       boolean getStatic)
                                throws Exception
        Render both dynamic and static JavaScript to perform validations based on the supplied form name.
        Parameters:
        formName - the key (form name)
        getStatic - indicates if the static methods should be rendered
        Returns:
        the Javascript for the specified form
        Throws:
        Exception
      • getDynamicJavascript

        protected String getDynamicJavascript​(org.apache.commons.validator.ValidatorResources resources,
                                              Locale locale,
                                              org.apache.commons.validator.Form form)
        Generates the dynamic JavaScript for the form.
        Parameters:
        resources - the validator resources
        locale - the locale for the current request
        form - the form to generate javascript for
        Returns:
        the dynamic javascript
      • escapeJavascript

        protected String escapeJavascript​(String str)

        Backslash-escapes the following characters from the input string: ", ', \, \r, \n.

        This method escapes characters that will result in an invalid Javascript statement within the validator Javascript.

        Parameters:
        str - The string to escape.
        Returns:
        The string s with each instance of a double quote, single quote, backslash, carriage-return, or line feed escaped with a leading backslash.
        Since:
        VelocityTools 1.2
      • createMethods

        protected String createMethods​(List<org.apache.commons.validator.ValidatorAction> actions)
        Creates the JavaScript methods list from the given actions.
        Parameters:
        actions - A List of ValidatorAction objects.
        Returns:
        JavaScript methods.
      • createActionList

        protected List<org.apache.commons.validator.ValidatorAction> createActionList​(org.apache.commons.validator.ValidatorResources resources,
                                                                                      org.apache.commons.validator.Form form)
        Get List of actions for the given Form.
        Parameters:
        resources - the validator resources
        form - the form for which the actions are requested
        Returns:
        A sorted List of ValidatorAction objects.
      • getJavascriptBegin

        protected String getJavascriptBegin​(String methods)
        Returns the opening script element and some initial javascript.
        Parameters:
        methods - javascript validation methods
        Returns:
        the opening script element and some initial javascript
      • getJavascriptStaticMethods

        protected String getJavascriptStaticMethods​(org.apache.commons.validator.ValidatorResources resources)
        Parameters:
        resources - the validation resources
        Returns:
        the static javascript methods
      • getJavascriptEnd

        protected String getJavascriptEnd()
        Returns the closing script element.
        Returns:
        the closing script element