Drupal allows you to take advantage of the Form API which is a framework for safely building, processing, displaying and validating HTML forms. It defines a form as a structured array that contains all the necessary data and information to handle the Drupal form. It is thus possible to customise a form within an individual module such as adding multiple fields which in turn allows designers and web development companies to customise screen display by overriding theme functions. This has also made it easier to validate form input and avoid tampering with forms especially in commercial web design.
Dynamic forms with Drupal 7 FAPI and form states however came as a trade off with the loss of flexibility in handling some complex scenarios such as how to handle dynamic forms that mutate based on input and multi-step forms that commonly cover more than one input page. This was however eliminated after FAPI 2 that was released with Drupal 5. Dynamic Forms with Drupal 7 FAPI and Form States present different scenarios for web development companies in commercial web design.
1. Long forms divided into multiple steps for easier use.
2. Form Definition Arrays.
3. Unique form ID and it's definition array.
4. A form's post data which depending on the form ID will determine forms processing, validation and submit stages and what processes will take place in each of those stages.
5. HTML generation, url passing or validation errors depending on the type of form POST data returned or the presence or absence of POST data.
6. Form user feedback.
7. Form submit sequence.
Dynamic forms, unlike static forms also present certain challenges or hurdles to a web designer that are unique. A static form simply entails building a data array, checking POST values, validating data and finally submitting the form with the option of rendering the form data for display.
The following problematic scenarios are possible.
1. A starting point is auto-generated for your form by a page building function.
2. The drupal get form function renders and displays the form.
3. Users fill the form and submit.
4. The page building function creates a form based on POST values.
5. After submitting the form, the form fields no longer match the form values.
The solution is to process form values as a single array. Drupal offers three simple solutions to the above possible scenarios.
1. Building forms using a dedicated form function and only submitting a form ID for form processing.
2. Form functions now allows editing parameters such as the node object and also allows specific values to represent user submitted data3.
Drupal now saves form parameters when building a form array. On subsequent submissions, the previously saved form parameters are now used to process and display the new form. The last item in this list is undoubtedly something web development companies will appreciate. After creating your form and the form building function, the get form function handles the correct parameters for each form submission phase.
Dynamic forms with Drupal 7 FAPI and form states therefore presents a unique and much tidier way to handle dynamic forms .
Subscribe to Dazines Web Development and Design to receive more tips on programming!