1 form_example_tutorial.inc form_example_tutorial_3($form, &$form_state)

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

File

modules/examples/form_example/form_example_tutorial.inc, line 102
This is the Form API Tutorial, originally from the Backdrop 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;
}