Building Individual Fields (Form API 6.x)

Last modified: May 7, 2009 - 20:05

An individual field is specified as an array of properties that define the field. The name of the element, by which it will be referred to in the form workflow (and in the rendered HTML,) is the key for which this array is inserted as a value in a containing array.

For example, to create a textfield element in a form, you would use the following properties (only the type property is required to create a textfield):

$form['my_text_field']=array( '#type' => 'textfield',
  '#title' => t('Enter some text here:'),
  '#default_value' => $node->title,
  '#size' => 60,
  '#maxlength' => 128,
  '#required' => TRUE
);

A full list of how to specify the various input types of a form (radio, check boxes, drop-down selectors, etc.) is listed here: Input Types in the Forms API for Drupal 6.x

With just the input types, and a submit button, which is written as, for example:

$form['my_submit_button'] = array(
'#type' => 'submit',
'#value' => t('Import')
);

you can create most simple, one-stage, forms! You really don't need to learn much more to create forms, apart from how to deal with the form data once it is posted.

Remember to enclose all strings in the form that are going to be described externally within the t() function, so that your form can be translated within the Drupal framework!

 
 

Drupal is a registered trademark of Dries Buytaert.