ARCHIVE: Drupal 4.6 vs. Drupal 4.7 Form API Flowcharts

Last modified: April 15, 2009 - 00:39

The following shows a comparison in the way forms were done pre-4.7 and how they are done in Drupal 4.7. Attached below is a Dia file containing an editable version of the diagrams.

See the print-friendly version if images are being hidden by menus.

Drupal Pre-4.7 Form API

Example:

<?php
function example_menu($may_cache) {
 
$items = array();
  if (
$may_cache) {
   
$items[] = array(
     
'path' => 'example_page',
     
'callback' => 'example_page',
     
'title' => 'example page',
     
'access' => TRUE,
    );
  }
  return
$items;
}

function
example_page() {
 
$op = isset($_POST['op']) ? $_POST['op'] : '';
 
$edit = isset($_POST['edit']) ? $_POST['edit'] : '';
  if (
$op == t('Submit')) {
    if (!(
$edit['field_1'] >= 1 && $edit['field_1'] <= 2)) {
     
form_set_error('field_1', t('Enter a value between 1 and 2'));
    }
    if (!
form_get_errors()) {
     
db_query('INSERT INTO {example} (data) VALUES (%d)', $edit['field_1']);
     
drupal_goto();
    }
  }
 
$form = form_textfield(t('first textfield'), 'field_1',
    isset(
$edit['field_1']) ? $edit['field_1'] : 1, 60, 255,
   
t('Enter a value between 1 and 2 ')
  );
 
$form .= form_submit(t('Submit'));
  return
form($form);
}
?>

Drupal 4.7 Form API

Example:

<?php
function example_menu($may_cache) {
 
$items = array();
  if (
$may_cache) {
   
$items[] = array(
     
'path' => 'example_page',
     
'callback' => 'example_page',
     
'title' => 'example page',
     
'access' => TRUE,
    );
  }
  return
$items;
}

function
example_page() {
 
$form['field_1'] = array(
   
'#type' => 'textfield',
   
'#default_value' => 1,
   
'#title' => 'first textfield',
   
'#description' => t('Enter a value between 1 and 2'),
  );
 
$form['field_2'] = array(
   
'#type' => 'submit',
   
'#value' => t('Submit')
  );
 
drupal_get_form('example_form_id', $form);
}

function
example_form_id_validate($form_id, $form_values) {
  if (!(
$form_values['field_1'] >= 1 && $form_values['field_1'] <= 2)) {
   
form_set_error('field_1', t('Enter a value between 1 and 2'));
  }
}

function
example_form_id_submit($form_id, $form_values) {
 
db_query('INSERT INTO {example} (data) VALUES (%d)', $form_values['field_1']);
 
drupal_goto();
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.