How do you overcome the HTML form nesting limitation?

I know that XHTML doesn’t support nested form tags and I have already read other answers here on Stack Overflow regarding this subject, but I still haven’t figured out an elegant solution to the problem.

Some say you don’t need it and that they can’t think of a scenario were this would be needed. Well, personally I can’t think of a scenario that I haven’t needed it.

Let’s see a very simple example:

You are making a blog app and you have a form with some fields for creating a new post and a toolbar with “actions” like “Save”, “Delete”, “Cancel”.

<form
 action="/post/dispatch/too_bad_the_action_url_is_in_the_form_tag_even_though_conceptually_every_submit_button_inside_it_may_need_to_post_to_a_diffent_distinct_url"
method="post">

    <input type="text" name="foo" /> <!-- several of those here -->
    <div id="toolbar">
        <input type="submit" name="save" value="Save" />
        <input type="submit" name="delete" value="Delete" />
        <a href="https://stackoverflow.com/home/index">Cancel</a>
    </div>
</form>

Our objective is to write the form in a way that doesn’t require JavaScript, just plain old HTML form and submit buttons.

Since the action URL is defined in the Form tag and not in each individual submit button, our only option is to post to a generic URL and then start “if…then…else” to determine the name of the button that was submitted. Not very elegant, but our only choice, since we don’t want to rely on JavaScript.

The only problem is that pressing “Delete”, will submit ALL the form fields on the server even though the only thing needed for this action is a Hidden input with the post-id. Not very big deal in this small example, but I have forms with hundreds (so to speak) of fields and tabs in my LOB applications that (because of requirements) have to submit everything in one-go and in any case this seems very inefficient and a waste. If form nesting was supported, I would at least be able to wrap the “Delete” submit button inside it’s own form with only the post-id field.

You may say “Just implement the “Delete” as a link instead of submit”. This would be wrong in so many levels, but most importantly because Side-effect actions like “Delete” here, should never be a GET request.

So my question (particularly to those that say they haven’t needed form nesting) is What do YOU do? Is there any elegant solution that I’m missing or the bottom line is really “Either require JavaScript or submit everything”?

16 Answers
16

Leave a Comment