Uploaded image for project: 'Velocity Tools'
  1. Velocity Tools
  2. VELTOOLS-52

ValidatorTool javascript generator can generate invalid Javascript



    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 1.2
    • 1.2
    • VelocityStruts
    • None
    • Using JDK1.4.2 / Linux 2.4 kernel / Tomcat 4.1


      ValidatorTool can create invalid javascript in a few situations.

      Here is an example of such a situation and also an example of the invalid javascript it generates.

      Suppose you have the following dynamic action form validation rules defined (this is actually text field which is intended to be used as an "other" input when a drop-down has the value of "Other").

      <field property="selectOther"
      <arg0 key="prompt.selectOther"/>
      <arg1 name="maxlength" key="${var:maxlength}" resource="false" />
      (((select == "Other") and (this != null)) or
      (select != "Other"))

      When ValidatorTool generates Javascript for this, you get the following:

      this.a3 = new Array("orgTypeOther", "The field Organization Type cannot be greater than 255 characters.", new Function ("varName", "this.maxlength='255'; this.test='(((orgType == "Other") and (this != null)) or
      (orgType != "Other"))'; return this[varName];"));

      Note that there is a newline in the string literal (invalid) and that the double-quotes used in my "validwhen" rule have not been escaped, which prematurely ends the double-quoted string starting with <code>"this.maxlength</code>, which really confuses the Javascript interpreter.

      It turns out that switching from double-quotes to single-quotes doesn't help, since there are also single-quoted strings within that double-quoted string, so basically it won't work no matter what you do (since backslash-escaping the quotes will cause the validwhen test itself to become invalid.

      I see two solutions: properly escape the variable values being dumped into Javascript, or avoid adding the "test" variable to the Javascript, since it will be ignored, anyway.

      I propose fixing the escaping, since there may be other validator "var" values with this same problem.


        1. ValidatorTool.diff
          4 kB
          Christopher Schultz



            nbubna Nathan Bubna
            chris@christopherschultz.net Christopher Schultz
            0 Vote for this issue
            1 Start watching this issue