views:

28

answers:

1

I've created custom form using FAPI for my site. And I place each control at specific location base on template provided by the designer. For instance -

<div id="myform">
  <span>Enter Your Name : </span> <?php print drupal_render($form['name']); ?>
  <span>Gender : </span><?php print drupal_render($form['gender_radio']); ?>
  ....
</div>
 <?php print drupal_render($form['submit']); ?>

Here's my question - How do I enclose all the elements inside form tag? Is hardcoding the form tag inside the template file right way to do in drupal? or is it better to create in hook_form? But doing so would require me to add closing form tag at the end manually. Any suggestion would be highly appreciated.
Drupal - 6.x

+1  A: 

It sounds like maybe you read about building individual fields, but skipped over some basic concepts of FAPI. In short, if you call the form with drupal_get_form(), you get the form container (and many of the benefits of using FAPI, e.g. tokens, validation, etc.) automatically. To handle the markup that goes around your form elements, you can then use #prefix, #suffix, and markup elements.

You can assemble the whole form from the outside in like you're doing, but there are few cases in which that would really be worthwhile. If you really want to do that, you basically want to copy what drupal_get_form() does to get the form wrapper added in a way that will work with FAPI.

Scott Reynen
I make it work by following your suggestion. Thanks a bunch. But one more question though. As I'm using template for my site(as oppose to theme function), I added drupal_get_form call inside template preprocessor function. It does do the trick. But I like to know if it is what drupal suggest. Cuz, normally, this function call is place inside theme function for those who use one and I'm using template and not sure whether inside preprocessor is what drupal suggest. And thanks also for the links. I read about forms just from drupal themes guide and drupal site didn't come to my mind at that time
Andrew
It's hard to say where your drupal_get_form call should be without knowing exactly what you're doing. In general, complex logic should stay out of template files, so preprocess functions would be better. But depending on what you're doing, it may make more sense to do the FAPI stuff in a module rather than a theme.
Scott Reynen