Issue Details (XML | Word | Printable)

Key: STR-899
Type: Bug Bug
Status: Resolved Resolved
Resolution: Won't Fix
Priority: Major Major
Assignee: Struts Developers
Reporter: Matt Raible
Votes: 0
Watchers: 0
Operations

If you were logged in you would be able to see more operations.
Struts 1

adding focus to the <html:form> tag with two forms of the same name generates a javascript error

Created: 09/Oct/02 08:46 PM   Updated: 11/Apr/07 08:21 PM
Component/s: Tag Libraries
Affects Version/s: 1.1 Beta 2
Fix Version/s: None

Environment:
Operating System: other
Platform: Other

Bugzilla Id: 13454


 Description  « Hide
If you have a JSP with 2 forms of the same name (action) - the javascript
rendered results in a javascript error.

To solve - change this:

<script language="JavaScript" type="text/javascript">
  <!--
 if (document.forms["uploadFiles"].elements["uploadedFile"].type != "hidden")
    document.forms["uploadFiles"].elements["uploadedFile"].focus()
  // -->
</script>

To something like:

<script language="JavaScript" type="text/javascript">
  <!--
 if (document.forms[1].elements["uploadedFile"].type != "hidden")
    document.forms[1].elements["uploadedFile"].focus()
  // -->
</script>

So you'd have to know the number of forms on the page. Also, the
language="Javascript" is not needed (it's invalid for XHTML), just the type.

 All   Comments   Change History   Subversion Commits      Sort Order: Ascending order - Click to sort in descending order
David Graham added a comment - 09/Oct/02 09:11 PM
Is it valid html to have 2 forms with the same name on a page? Why would you do this?

I agree with
the language attribute being removed for xhtml compliance but the rest of the tag isn't xhtml
compliant to begin with (ie. <SCRIPT> instead of <script>).

Matt Raible added a comment - 09/Oct/02 10:01 PM
Actually, according to the spec - all tags should be lowercase:

http://www.w3.org/TR/xhtml1/#h-4.8

Therefore, it should be <script...>

Also, I ran the following through the validator at http://validator.w3.org and
it no errors were found for having 2 forms of the same name.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Roller :: Editor</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<div id="content"><h3>Upload File</h3>
<form name="uploadFiles" method="post"
action="/roller/uploadFiles.do?method=upload"
            enctype="multipart/form-data"> Select an file from your
computer<br/>
<input type="file" name="uploadedFile" size="30"/><br/>
<input type="submit" value="Submit"/>
</form><h3>Manage Files</h3>
<form name="uploadFiles" method="post"
action="/roller/uploadFiles.do?method=delete">
<table class="rTable">
<tr class="rHeaderTr">
<td>Filename</td>
<td>Size</td>
<td>Delete</td>
</tr>
<tr class="rEvenTr">
<td class="rTd" colspan="3">No Files
Found</td>
</tr>
</table>
<input type="submit" value="Delete"></input>
</form>
        
    <script type="text/javascript">
    <!--
        document.forms[0].elements[0].focus();
    // -->
    </script>
</div>
</body>
</html>

Matt Raible added a comment - 09/Oct/02 10:06 PM
You are correct if you use the DOCTYPE XHTML 1.0 Strict - you not only have to
replace the "name" attribute from "form" with and "id" attribute, you you can
only have one "id" per unique identifier per page.

chanoch added a comment - 13/Oct/02 05:50 AM
in my development environment this happens a lot - because of the way that the app is organized,
there are several places where a page will have several forms that utilize the same Form bean,
hence they are all called the same. A potential solution would be the adding of an optional id
attribute as requiring knowledge of the number of forms in the page and depending on this not
changing makes the resultant jsps rather brittle

David Graham added a comment - 13/Oct/02 06:29 AM
It's still not clear to me whether it's valid to have 2 forms of the same name. My previous post meant
that the tag uses <SCRIPT> now and should use <script>...I think you read it backwards :-).

Ted Husted added a comment - 13/Oct/02 06:40 AM
It's been my experience that the best way to set the focus is to refer to the
forms and field by number. If we did have a script that allowed us to set the
form and field number, it could also fix the issue with the focus script not
working in every browser. If anyone has submitted a path for setting the focus
by number rather than name, please call my attention to it. -Ted.

chanoch added a comment - 18/Oct/02 06:32 AM
I've been thinking about this and need clarification if you dont mind Ted - is the 'designer'
calling the form number or do we need to figure this out in code?

i.e. do we require the user to
enter something like:

<html:form action="/ProcessThis" focus="[n].lastname">
 
<html:text property="firstname"/>
 <html:text
property="lastname"/>
</html:form>

Assuming yes:
is this a zero based index?
is
there a better format?

If not:
Anyone give me a clue on how to go about finding out the index
number of the form automatically

Finally:
I know this is tiresome but can you say yay or nay
on whether there is a reason why I cant add an optional id attribute that is passed straight on to the
resultant HTML and that therefore uniquely identifies the form so no form counting shennanigans
are necessary?

ta

chanoch

Ted Husted added a comment - 19/Oct/02 06:57 AM
I don't know if there's a good way to find the form number automatically. To be honest, I really
think it's simplest just to code the JavaScript outside of tag.

If you want to count the forms,
that's easy enough to do with scriplets,

<% int form=0;%>

/... form[<%=form%>]

<%
form++; %>

or to just maintain by hand.

IHMO, some of this seems to transcend what might
reasonably be expected of the lowly form tag. The focus= is a nice little gimme, but it my
experience, it doesn't work in a lot of cases, so I don't even use it myself. It really doesn't do
anything you couldn't do just as easily yourself.

If someone were to submit a patch for a
script that worked better, I'm sure it would be worth considering, but unless it can determine the
script number automatically, it might be better to leave the more complex focus use-cases as an
exercise for the developer. (Though, as a courtesy, we could document some techniques for
alternate ways to set the focus.)

There's also use-cases such as changing the focus after they
use an element, like

<html:select size="1" property="dispatch"
onchange="document.forms[0].elements[1].focus()">

Should this be something that the
tags should be doing too?

I just think after the simplicity of the focus= tag, we start down a
slipperly slope. So, I'm not saying no, I'm just asking why? (Hey, but if you fix it, send the
patch!)

Ted Husted added a comment - 19/Oct/02 08:44 AM
*** STR-823 has been marked as a duplicate of this bug. ***

James Mitchell added a comment - 05/Nov/02 02:20 PM
I've been looking at this bug off and on for the last few days, and in addition
to Ted's comments, no one has mentioned the fact that if you are creating
multiple forms (with focus) as part of some iteration, each form will have a
<script> right after the ending </form>.

The only way to avoid this is to test during jsp execution, and if you are doing
that, you might as well do it the right way (not use duplicate names).

I've also compiled a nice collection of sample pages for testing all the
possible choices.

With duplicate forms with same name:
 NN4 sets the focus to the correct field on the last form
 NN6 sets the focus to the correct field on the first form
 Mozilla sets the focus to the correct field on the first form
 IE6 does not set anything and gives a script error message

This issue is obviously handled very different among browsers and should not
take up any more valueable time.

Unless someone can submit a patch that works consistently and DOES NOT reduce
the current functionality, this bug should remain closed.

Ted Husted added a comment - 11/Jul/06 09:05 PM
One alternative is described here:

* http://www.codeproject.com/jscript/FocusFirstInput.asp

Radek Mensik added a comment - 11/Apr/07 08:21 PM
My solution, how to fix SAME NAMES IN ALL FORMS connected with same ACTION is : if the name of form is wrong, just try all forms, if they containt the element you wanna update.
Example : Replace this
function showCalendar(year, month, day, pattern, formName, formProperty, event, startYear, endYear) {
  
  
  if (document.forms[formName].elements[formProperty].disabled) {
return;
}

and fill the while cycle to find correct form

function showCalendar(year, month, day, pattern, formName, formProperty, event, startYear, endYear) {
i=1
  while(document.forms[formName].elements[formProperty]==null&&i<20){
  formName=i;r
  i++;
  }
  
  if (document.forms[formName].elements[formProperty].disabled) {
return;
}