The Holy Grail of Front-End Development
Join the DZone community and get the full member experience.Join For Free
if you aim to run a respectable front-end blog there are certain subjects that need to be tackled in order to build up credibility. ie bitching is a popular one, whining about lacking css support in browser x another, but there is one subject that could be considered the holy grail of front-end dev. a subject that most of us fear and dislike, but is still one of the absolute cornerstones of the web today: forms. be it styling or mark-up, forms remain a fickle element of our job.
the form attribute
sure i've talked about forms before, but never about the real thing . i wrote a piece on tabular forms and a quick css post on styling equal height forms , but the traditional, full-length, expanded form i've tried to avoid so far. the reason is pretty straight-forward: i've never really felt at ease with the code i've been using. that's not to say that the code i will deliver in this article is absolutely perfect, but at least it shows some interesting connections to other popular web components and it serves as a solid starting point for future improvements.
learning from the mistakes of others
it's ironic how glaring mistakes can sometimes uncover core truths about particular problems. if you've been doing front-end development long enough, you might remember the days when back-end developers represented a data sheet (typical lists of label/value pairs on detail pages) as a form with the input fields set to disabled. this particular structure made their work a lot easier as they simply had to remove the disabled attributes from the input fields to put the data sheet in edit mode (creating a regular form).
while all of that sounds pretty silly nowadays, there is still something to be said for their line of reasoning. both data sheet and form are basically the same semantic entity, a form simply being the edit mode of a data sheet. this semantic link between both elements is very real and obvious, so in all fairness this link should be equally apparent in our html code. after all, semantics and structure is what html is all about.
with that in mind, we can adequately define the challenge ahead: come up with a piece of html code that can handle the specificities of both data sheets and forms while keeping the differences to a minimum. html minimalists beware, the result might be a little too verbose to your liking, but we're not aiming for minimalism here.
an old (but popular) way of marking up forms is by using tables. this has actually become somewhat of an accepted practice as most screen reader programs feature specific table-form modes which guides their users through the unsemantic mess. taking into account the mark-up for data sheets though, tables are simply not a valid option here. we're not going to use tables to mark up label/value pairs, on top of that i'm not very happy with using tables for form mark-up either.
so what's the best way to mark-up label/value pairs then? well, according to the html5 spec, the dl-dd-dt structure got a recent overhaul to encompass exactly that. it's a reasonably elegant solution for simply data sheets, but if you consider all the extras a regular form needs (user feedback, input hints/help, multiple inputs on a single row) then the drawbacks of the parentless dd-dt pairs become a real pain in the ass. the structure simply doesn't allow us enough flexibility for styling, nor does it provides us with the means of building a logical html structure. just another reason why i have my reservations about the proposed structure of definition lists.
so all that is left is to build our own data sheet/form structure using divs and some proper classes. let's give it a shot.
<section class="datasheet (editable)"> (<form id="formid" action="#" method="#">) <header> (heading/form feedback/required indication) </header> <div class="main"> (label/value pairs) </div> <footer> (crud links/options/submit) </footer> (</form>) </section>
what we have here is a very typical (and generic) component setup. the optional .editable class serves as the form mode toggle, apart from that we use a simple base class for our data sheet component. note that the form tags are added only in .editable mode. if you're a real purist you could argue that the header shouldn't be wrapped inside the form tag (as it should not hold any input elements), but that would take us a little too far. i left the header inside the form tags as it puts it on the same structural level as the main and footer containers, which feels more natural to me.
the header and footer elements are not always necessary, but they come in pretty handy to separate the label/value pairs from action-related components and/or meta data. the header can be used for headings, form feedback (form error overview or general help) and the classic required indicator explanation. the footer can be used for submit buttons and cancel links in form mode, or for the typical crud options in data mode. and if the design/css doesn't permit it, the crud options can always be added to the header instead.
it's a generic setup i use for many components that have unpredictable and varying degrees of complexity. it's nothing much out of the ordinary so far.
<section class="fieldset"> <h1> ... </h1> </section>
<fieldset> <legend> ... </legend> </fieldset>
as you can see, the tags change according to their context, but the structure remains the same. for ease of styling you can add an extra wrapper div following the legend/h1 which can be used for better cross-browser padding/margin control (fieldsets are notoriously hard to style). similarly, you can nest a span inside the legend tag for some cross-browser pos:abs magic, but those are all cosmetic changes and have little to do with semantics and/or structure.
this part is of course optional, if your data sheet/form doesn't have any subdivisions then you won't need fieldsets or extra sectioning elements. to make the implementation easier you can also opt to drop the fieldset syntax and always use the sections, that way no extra work is needed when switching between data and edit mode.
wimpy html concessions
<div class="row"> ... <div class="feedback (error) (confirmation)"> ... </div> </div>
the html bit above is something i would rather avoid using, but sadly it's a necessary bit of code if you want to build a little flexibility in your form design. i add it to ease the process of fitting multiple label/value pairs (think first/last name or city/postal code) on one single line. its semantic value is rather vague (sometimes the pairs are linked, sometimes it's just a matter of saving space), but there's still a practical side to web design that has to be taken into account. it's also the easiest way to provide immediate input feedback, which happens on row-level and not on pair-level. i know this is not ideal, but fitting multiple pairs on one line and providing pair-specific feedback is virtually impossible in most setups.
label/value pairs - finally!
<div class="spec (inputtype)"> <div class="label"> (<label for="id">)...(</label>) </div> <div class="value"> ... </div> </div>
and so we finally reach the code for our label/value pairs. notice the seemingly obsolete div.label element, which is used to allow extra hint/help information to go together with the label. you could of course nest the extra info inside the label element, but that way it is always included for screen readers, which can be a bit much at times. at least this way you have the choice, only adding it where needed.
within the div.value you can either add the actual data or the input controls + all the extra typical form tidbits (calendar overlays and help flyouts) that go with the input element.
putting all that together, you get a pretty mean piece of html code. from a semantical and structural point of view it's a rock-solid, flexible and reusable code setup, but i'm aware that it brings some overhead and it's rather verbose. if minimalism is your html ideal, this is definitely not for you.
the cool thing though is that it provides a very generic solution to capture the semantics and structure of data sheets and forms with a minimum of impact on the actual code. not only is it easy to implement, it also makes a lot of sense from a semantical point of view. in its easiest setup the only things that change are the extension class on the root elements, the addition of form tags, the extra label tag and the input elements. to make it a bit more specific there's some extra work to convert fieldsets, but that's about all there is.
not only will developers be grateful (unless they're using auto-generated cms form code), it actually makes a lot of semantic sense to work like this. this kind of abstraction always leads to a little overhead, but it's a price i'm willing to pay.
Opinions expressed by DZone contributors are their own.