multipage_form_example_form

5 multipage_form_example.module multipage_form_example_form(&$node)

Implementation of hook_form() for multipage_form_example. We don't set ANY #required attributes here - we leave that up to multipage_form_example_pre_render().

File

developer/examples/multipage_form_example.module, line 51

Code

function multipage_form_example_form(&$node) {
  $form = array();

  // 'checkboxes' elements are just ridiculously hard--i recommend just
//  using a bunch of individual checkbox elements instead.
//  I suspect that multiselects would also be pretty difficult...

  // Here's a new form element, which enables you to store hidden values
  // in an array.  Check out the element definition functions at the bottom
  // if you want to use this in a module.  Keys show up in the
  // $_POST/$form_values like:
  // $_POST['edit']['test_hidden_array']['n'] = 'north'
  // $form_values['test_hidden_array']['n'] = 'north'

  $form['test_hidden_array'] = array(
    '#type' => 'hidden_array', 
    '#values' => array(
      'n' => 'north',
      'e' => 'east',
      'w' => 'west',
      's' => 'south',
    ),
  );



  // Helper for our multipage - does field switching, sets options
  // based on validated $form_values instead of $_POST, and so forth.
  $form['#pre_render'] = array('multipage_form_example_pre_render');

  // Title and body, page 1
  $form['title'] = array(
    '#type' => 'textfield', 
    '#title' => t('Title'), 
    '#default_value' => isset($node->title) ? $node->title : NULL, 
    '#description' => t("Enter a title for your favorites section."), 
    '#size' => 60, 
    '#maxlength' => 128, 
    '#required' => TRUE,
  );
  $form['body'] = array(
    '#type' => 'textarea', 
    '#title' => t('Description'), 
    '#default_value' => isset($node->body) ? $node->body : NULL, 
    '#description' => t('Add any additional info about your favorites.'), 
    '#rows' => 20,
  );

  // Person, page 2
  $form['person'] = array(
    '#type' => 'fieldset', 
    '#title' => t('Your favorite person'),
  );
  $form['person']['fav_person'] = array(
    '#type' => 'textfield', 
    '#title' => t('Name'), 
    '#default_value' => isset($node->fav_person) ? $node->fav_person : NULL, 
    '#description' => t('Enter their real name, or their code name if they like to fly stealth.'), 
    '#required' => TRUE,
  );
  $form['person']['fav_person_desc'] = array(
    '#type' => 'textarea', 
    '#title' => t('Description'), 
    '#default_value' => isset($node->fav_person_desc) ? $node->fav_person_desc : NULL, 
    '#description' => t('Juicy details go here...'), 
    '#required' => TRUE,
  );
  $form['person']['fav_gummi'] = array(
    '#type' => 'checkbox', 
    '#default_value' => isset($node->fav_gummi) ? $node->fav_gummi : 0, 
    '#title' => t('Do they like gummi bears?'),
  );

  // Color and number, page 3
  $form['fav_color'] = array(
    '#type' => 'select', 
    '#title' => t('Favorite color'), 
    '#default_value' => isset($node->fav_color) ? $node->fav_color : 'red', 
    '#options' => array(
      'red' => t('Red'),
      'green' => t('Green'),
      'blue' => t('Blue'),
      'yellow' => t('Yellow'),
    ), 
    '#required' => TRUE,
  );
  $form['fav_number'] = array(
    '#type' => 'textfield', 
    '#title' => t('Favorite number'), 
    '#default_value' => isset($node->fav_number) ? $node->fav_number : NULL, 
    '#required' => TRUE,
  );

  // Movie and tv show, page 4
  $form['fav_movie'] = array(
    '#type' => 'textfield', 
    '#title' => t('Favorite movie'), 
    '#default_value' => isset($node->fav_movie) ? $node->fav_movie : NULL, 
    '#required' => TRUE,
  );
  $form['fav_tv'] = array(
    '#type' => 'radios', 
    '#title' => t('How often do you watch your favorite tv show?'), 
    '#default_value' => isset($node->fav_tv) ? $node->fav_tv : 'daily', 
    '#options' => array(
      'daily' => t('Daily'),
      'weekly' => t('Weekly'),
      'monthly' => t('Monthly'),
    ), 
    '#required' => TRUE,
  );

  // Add a back button
  $form['back'] = array(
    '#type' => 'button', 
    '#value' => t('Back'), 
    '#weight' => 35,
  );

  return $form;
}
Login or register to post comments