You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by bu...@apache.org on 2005/05/30 21:05:19 UTC

DO NOT REPLY [Bug 35127] New: - All Javascript validation fails when is present

DO NOT REPLY TO THIS EMAIL, BUT PLEASE POST YOUR BUG�
RELATED COMMENTS THROUGH THE WEB INTERFACE AVAILABLE AT
<http://issues.apache.org/bugzilla/show_bug.cgi?id=35127>.
ANY REPLY MADE TO THIS MESSAGE WILL NOT BE COLLECTED AND�
INSERTED IN THE BUG DATABASE.

http://issues.apache.org/bugzilla/show_bug.cgi?id=35127

           Summary: All Javascript validation fails when <html:xhtml/> is
                    present
           Product: Struts
           Version: 1.2.7
          Platform: All
        OS/Version: other
            Status: NEW
          Severity: major
          Priority: P2
         Component: Validator Framework
        AssignedTo: dev@struts.apache.org
        ReportedBy: zarar.siddiqi@utoronto.ca


If <html:xhtml/> is enabled, any form rendered via <html:form/> does not print 
a "name" attribute in Struts 1.2.7.  This is all well and good.  However, the 
static javascript that is printed using the following code:

<html:javascript dynamicJavascript="false" staticJavascript="true"/>

will all fail since, it utilizes the name attribute.  Here's an example:

    function validateRequired(form) {
        var isValid = true;
        var focusField = null;
        var i = 0;
        var fields = new Array();
        var formName = form.getAttributeNode("name");

        oRequired = eval('new ' + formName.value + '_required()');
        // more code here
    }

The above will fail in all cases since it's looking for a name attribute which 
doesn't exist.  The above Javascript is coming from the Commons Validator 
library being invoked from the JavascriptValidatorTag class.

A related issue is the focus field feature which also fails since it relies on 
the name attribute.  

I've implemented a solution to this:

The following code prints out javascript that focuses on the province field.
        <html:form action="/person/addressSave" focus="province"
                onsubmit="return validateAddressForm(this);">

Here is the generated focus code:

<script type="text/javascript">
  var focusControl = document.forms["addressForm"].elements["terminationDate"];

  if (focusControl.type != "hidden" && !focusControl.disabled) {
     focusControl.focus();
  }
</script>


The above will fail since "addressForm" is not printed in the <form> tag.

My solution utilizes the id attribute instead. Here's the diff in the FormTag 
class.  So in short, if Xhtml is enabled, use the id attribute and if it's not, 
use the name attribute.  Change the focus code accordingly.

505a506,507
>         } else {
>             renderId(results);
534a537,544
>      * Renders the id attribute
>      */
>     protected void renderId(StringBuffer results) {
>         results.append(" id=\"");
>         results.append(beanName);
>         results.append("\"");
>     }
>     /**
548c558
< 
---
>                 
659,663c669,681
<         StringBuffer focusControl = new StringBuffer("document.forms[\"");
<         focusControl.append(beanName);
<         focusControl.append("\"].elements[\"");
<         focusControl.append(this.focus);
<         focusControl.append("\"]");
---
>         StringBuffer focusControl = new StringBuffer();
>         if (isXhtml()) {
>             focusControl.append("document.getElementById(\"");
>             focusControl.append(beanName);
>             focusControl.append("\").");
>             focusControl.append(this.focus);
>         } else {
>             focusControl.append("document.forms[\"");
>             focusControl.append(beanName);
>             focusControl.append("\"].elements[\"");
>             focusControl.append(this.focus);
>             focusControl.append("\"]");
>         }



MY TWO CENTS:

It's probably a good idea to revert the changes made when fixing the following 
bug:

http://issues.apache.org/bugzilla/show_bug.cgi?id=34027

Although the change is correct, it simply breaks too many other things for it 
to be adopted at this point.

-- 
Configure bugmail: http://issues.apache.org/bugzilla/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
You are the assignee for the bug, or are watching the assignee.

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org