header photo

via positiva

tutorial

Dynamic and Multi-Step With Drupal's Form API

The release of Drupal 4.7 introduced the Form API, a framework for building, displaying, validating, and processing HTML forms. With it, forms are defined as structured arrays, and those structured arrays contain all the information necessary to properly handle the form throughout its life cycle. This approach also makes it possible for modules to customize other forms (adding additional fields to a signup page, for example), and allows designers to customize the on-screen display of forms using overridable theming functions. It also makes validating form input, and avoiding form tampering, much easier. That's great!

The tradeoff of those enhancements was the loss of flexibility in certain complex scenerios -- in particular dynamic forms that change based on user input, and multi-step 'wizard' style forms. With the introduction of Form API 2.0 in version 4.8/5.0 of Drupal, though, we've eliminated the limitations that made those cases so difficult.

Dynamic Forms: Three Different Scenerios

For the purposes of this article, we'll be looking at three specific kinds of dynamic forms:

  1. The Long Form, a large form divided into multiple steps for ease of use
  2. The Wizard, a form where a user's answers in each step determine the options available in the next
  3. The Form That Builds Itself, a page where some options (clicking 'add more choices' when setting up a poll, for example) add more fields until the user chooses to submit the 'finished product.'

While the first scenerio isn't really dynamic (it simply presents different subsets of options to the user at each step along the way), all of these scenerios work by changing the form depending on the data that the user has just submitted. And that means that all three scenerios encounter the same options in the current Form API. Why is that? Read on...  Continue reading...

Syndicate content

Miniblog

  • RT @netaustin I suspect that #NoSQL's success is largely due to lack of innovation by MySQL post-acquisition. 3 hours ago
  • Two days to the #sxsw panel on Open Source: 'Selling milk when the cow is free.' What questions do YOU want to sneak into the queue? 19 hours ago
  • OH: "'Middle class' means being able to waste an hour arguing about the definition of middle class with your college-educated friends." 23 hours ago
  • Dear #drupalweb: Any designers out there familiar with whipping up Zen-based pure CSS skins? 1 day ago
  • "New drinking game! take a drink every time eaton says 'canonical!'" Truly, @lizak knows how to get a crowd drunk FAST. 3 days ago

SXSW Interactive 2010!

Come to the 2010 CMS Expo