Same name and namespace in other branches
  1. 6.x-1.x form_example/form_example_tutorial.inc \form_example_tutorial_3()

Example 3: A basic form with fieldsets.

We establish a fieldset element and then place two text fields within it, one for a first name and one for a last name. This helps us group related content.

Study the code below and you'll notice that we renamed the array of the first and last name fields by placing them under the $form['name'] array. This tells Form API these fields belong to the $form['name'] fieldset.

Related topics

1 string reference to 'form_example_tutorial_3'
form_example_menu in form_example/form_example.module
Implements hook_menu().

File

form_example/form_example_tutorial.inc, line 102
This is the Form API Tutorial from the handbook.

Code

function form_example_tutorial_3($form, &$form_state) {
  $form['description'] = array(
    '#type' => 'item',
    '#title' => t('A form with a fieldset'),
  );
  $form['name'] = array(
    '#type' => 'fieldset',
    '#title' => t('Name'),
  );
  $form['name']['first'] = array(
    '#type' => 'textfield',
    '#title' => t('First name'),
  );
  $form['name']['last'] = array(
    '#type' => 'textfield',
    '#title' => t('Last name'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
  );
  return $form;
}