views:

60

answers:

1

What is the right way of creating and processing a group of related controls on posted forms?

From using ASP.NET MVC I see a standard option is to create a number of inputs and assign the same "name" attribute value to them. Like this:

<input name="OrderOptions" type="checkbox" value="1" />
<input name="OrderOptions" type="checkbox" value="2" />
...
<input name="OrderOptions" type="checkbox" value="N" />

And when processing forms we get all the values in a comma delimited string:

public OrderController
{
    public ActionResult (FormCollection form)
    {
        string selection = form["OrderOptions"];
    }
}

Now, is this how it is supposed to be done with any server technology? Does assigning the same name value to inputs break some validation rules or something?

One extra question: If I were to use the built-in HTML helpers, I would get the inputs generated with both "id" and "name" attribute. Like this:

<input id="OrderOptions" name="OrderOptions" type="checkbox" value="1" />
<input id="OrderOptions" name="OrderOptions" type="checkbox" value="2" />
...
<input id="OrderOptions" name="OrderOptions" type="checkbox" value="N" />

But it is clearly invalid to have multiple elements with the same "id" in a document. Still, it works.

If I discard the standard helpers and make my own, do I need to insert the "id" attribute to inputs if I do not really need it (except in some label cases)? Some folks are telling we have to always assign both "id" and "name" attributes to elements because there is some incompatibility with old browsers, and the "name" attribute is deprecated (I know it is only for some other elements). But even if I wanted to, I would have to assign different id/name values for input elements and then I cannot process them as a group. You see my dilemma?

Any advice is greatly appreciated.

+1  A: 

a) No. Assigning the same name is definitely valid behaviour -- it's how radio buttons know which group they're a part of (so that others in the same group turn off when you click one, while other groups on the same page are unaffected).

b) Yes, having the same ID is invalid. I have the same problem with the helper apps. It makes the entire page invalid and, for me at least, makes any javascript more difficult.

No, you don't need the ID. But if they exist they should be unique. Furthermore, I don't know about this whole "name being deprecated" thing. How else will forms work? Forms do not submit the ID when POSTing back. See http://www.w3.org/TR/html401/interact/forms.html#edef-INPUT .

If you choose to assign ID, they can be different without affecting group_processing. In fact, I'll generally name them something like "OrderOptions-<%= order.option.id %>".

EDIT:

PS: Use the html validator at http://validator.w3.org/#validate_by_uri+with_options . It'll catch and notify you of things like duplicate IDs or missing IDs. It'll also (if I remember correctly) tell you of deprecated elements/attributes that you use.

James

James S