Input Tags In Forms Not Valid XHTML

(I'm stuck at Sky Harbor in Phoenix because of a delayed flight, but they have free wireless access, so I might as well make a blog post while I'm here. ;-)

Did you know the following is not valid XHTML?


<form action="index.php">
    <input type="hidden" name="foo" value="bar" />
</form>

It appears that an input tag is not allowed to appear directly within a form tag. See detailed information here and note that <input> is not a valid child element of <form>. To be valid XHTML, the input tag must be wrapped by another block-level element, such as <p> or <fieldset>.

I can safely say I have always done this the wrong way. ;-) For example, the Solar_View_Helper_Form class puts all hidden fields at the top of the form, and all other fields in a <dl>...</dl> block. As a result, the hidden fields cause the page to be invalid (but the others are valid because they are wrapped properly).

Solution? Wrap the hidden fields in a <fieldset style="display: none">...</fieldset> tag. Now the page validates, and the hidden fieldset doesn't alter the page layout at all. This change has been committed and will be available in the next release of Solar.

(UPDATE: Fixed the link to the XHTML spec; thanks, David Rodger.)

Are you stuck with a legacy PHP application? You should buy my book because it gives you a step-by-step guide to improving your codebase, all while keeping it running the whole time.