Thursday, September 29, 2011

Simple Form API Example

<?php
//Menu Hook
function formcreation_menu() {
$items = array();
$items['formcreation/new_form'] = array(
    'title' => t('New form'),
      'page callback' => 'drupal_get_form',
    'page arguments' => array('create_form'),
    'access arguments' => array('access content'),
    'description' => t('New form'),
    'type' => MENU_CALLBACK,
);
return $items;
}
// Menu Callback
function create_form($form_state) {
$form['personal_details'] = array(
    '#type' => 'fieldset',
    '#title' => t('Personal Details'),
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,
);
$form['personal_details']['first'] = array(
    '#type' => 'textfield',
    '#title' => t('First name'),
    //'#required' => TRUE,
    '#description' => "Please enter your first name.",
    '#size' => 20,
    '#maxlength' => 20,
);
$form['personal_details']['last'] = array(
    '#type' => 'textfield',
    '#title' => t('Last name'),
    //'#required' => TRUE,
    '#description' => "Please enter your last name.",
    '#size' => 20,
    '#maxlength' => 20,
);

$form ['personal_details']['year_of_birth'] = array(
    '#type' => 'textfield',
    '#title' => "Year of birth",
    '#description' => 'Format is "YYYY"',
    '#size' => 10,
    '#maxlength' => 4,
);
//Adding more fields for the form
if (isset($form_state['storage']['other_details'])) {
      $form['personal_details1'] = array(
        '#type' => 'fieldset',
        '#title' => t('More Details...'),
        '#collapsible' => TRUE,
        '#collapsed' => FALSE,
      );
      $form['personal_details1']['age'] = array(
        '#type' => 'textfield',
        '#title' => t('Age'),
        '#description' => "Please enter your Age.",
        '#size' => 3,
        '#maxlength' => 3,
        '#default_value' => $form_state['values']['age'],
      );
      $form['personal_details1']['sex'] = array(
        '#type' => 'textfield',
        '#title' => t('Sex'),
          '#description' => "Please enter your Sex.",
          '#size' => 6,
        '#maxlength' => 6,
        '#default_value' => $form_state['values']['sex'],
      );
      $form['personal_details1']['city'] = array(
        '#type' => 'textfield',
        '#title' => "City",
        '#description' => 'Please enter your City.',
          '#size' => 20,
        '#maxlength' => 20,
        '#default_value' => $form_state['values']['city'],
      );
    }
   
$form['submit'] = array(
    '#type' => 'submit',
    '#value' => 'Submit',
);
//Reset the form, Hook;
$form['clear'] = array(
    '#type' => 'submit',
    '#value' => 'Clear',
    '#validate' => array('create_form_clear'),
  );
  //Adding more fields, Hook;
if (empty($form_state['storage']['other_details'])) { 
      $form['other_details'] = array(
        '#type' => 'submit',
        '#value' => 'Add Some more Details',
        '#validate' => array('create_form_other_details'),
      );
    }
return $form;
}
//Form validation
function create_form_validate($form, &$form_state) {
    $year_of_birth = $form_state['values']['year_of_birth'];
    $first_name = $form_state['values']['first'];
    $last_name = $form_state['values']['last'];
        if (!$first_name) {
            form_set_error('first', 'Please enter your first name.');
        }
        if (!$last_name) {
            form_set_error('last', 'Please enter your last name.');
        }
        if ($year_of_birth == '') {
        form_set_error('year_of_birth', 'Kindly enter the year of Birth');
        }else if($year_of_birth < 1900 || $year_of_birth > 2000){
        form_set_error('year_of_birth', 'Enter a year between 1900 and 2000.');
        }
//validation for the extra fields that added newly
    if ($form_state['storage']['other_details']) {
        $age = $form_state['values']['age'];
        $sex = $form_state['values']['sex'];
        $city = $form_state['values']['city'];
        if (!$age) {
            form_set_error('age', 'Please enter your Age.');
        }
        if (!$sex) {
            form_set_error('sex', 'Please enter your Sex.');
        }
        if (!$city) {
            form_set_error('city', 'Please enter your City.');
        }
    }
}
//Adding more fields Menu Callback
function create_form_other_details($form, &$form_state) {
    $form_state['storage']['other_details'] = TRUE;
    $form_state['rebuild'] = TRUE;
}
//Reset the form Callback
function create_form_clear($form, &$form_state) {
    unset ($form_state['values']); 
    unset ($form_state['storage']);
    $form_state['rebuild'] = TRUE;
}
//Form Submit
function create_form_submit($form, &$form_state) {
unset($form_state['storage']);
drupal_set_message(t('The form has been submitted.'));
}

?>

No comments:

Post a Comment