Forms Control - interessante esempio per poter scrivere i vostri form dati

In questa pagina di documentazione viene riportato il codice per la costruzione di un form dati. Gli input form vanno dal button al campo data con intervallo prestabilito, il bottone submit e il bottone reset, il campo password e la textarea. Lo script sotto non è testato riporta solamente un esempio su come scrivere gli input form, la validazione e il ritorno di variabili.

<?php
/**
* Display help and module information
*/
function formdati_help($path, $arg) {
  $output = '';
  switch ($path) {
    case "admin/help#formdati":
      $output = '<p>'.  t("Allows you to show formdata inside the site") .'</p>';
      break;
  }
  return $output;
}
function formdati_init() {
  drupal_add_js(drupal_get_path('module', 'formdati') .'includes/formdati.js');
}
/**
* Valid permissions for this module
* @return array An array of valid permissions for the formdati module
*/
function formdati_perm() {
  return array('access formdati', 'post formdati', 'amminister formdati');
}
function formdati_menu() {
  $items = array();
   $items['formdati'] = array(
    'title' => t('Form data'),
    'description' => t('Allows you to show formdata inside the site'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('formdati_form'),
    'access arguments' => array('access formdati'),
    'weight' => 1,
    'type' => MENU_NORMAL_ITEM,
   );
  return $items;
}
function formdati_form() {
$node->type = 1;
$status = 0;
$category = 'Categories';
$field->name ='Namefield';
$form['#attributes']['enctype'] = 'multipart/form-data';
$form['#validate'][] = 'formdati_form_validate';
$form['#submit'][] = 'formdati_form_submit';
$form['contact'] = array(
  '#type' => 'fieldset',
  '#title' => t('Contact settings'),
  '#weight' => 1,
  '#collapsible' => TRUE,
  '#collapsed' => FALSE,
);
$form['preview'] = array(
  '#type' => 'button',
  '#value' => t('Preview'),
  '#weight' => 19,
  '#attributes' => array('onclick' => 'alert(this.name);'),
);
$form['cancel'] = array(
   '#type' => 'button',
   '#value' => t('Cancel'),
);
$form['reset'] = array('#value' => '<input type="reset" value="Reimposta">', '#attributes' => array('class' => 'form-submit'));
$form['copy'] = array(
  '#type' => 'checkbox',
  '#title' => t('Send me a copy.'),
  '#default_value' => $status,
  '#default_value' => variable_get('copy', 0),
);
$form['node_options_'. $node->type] = array(
  '#type' => 'checkboxes',
  '#title' => t('Default options'),
  '#default_value' => $status,
  '#default_value' => variable_get('node_options_'. $node->type, array('status', 'promote')),
  '#options' => array(
    'status' => t('Published'),
    'moderate' => t('In moderation queue'),
    'promote' => t('Promoted to front page'),
    'sticky' => t('Sticky at top of lists'),
    'revision' => t('Create new revision'),
  ),
  '#tree' => TRUE,
  '#description' => t('Users with the <em>administer nodes</em> permission will be able to override these options.'),
);
$fields[$category][$field->name] = array(
  '#type' => 'date',
  '#title' => check_plain($field->title),
  '#default_value' => $edit[$field->name],
  '#description' => _profile_form_explanation($field),
  '#required' => TRUE,
  '#default_value' => array('year' => '', 'month' => '', 'day' => ''),
);
$form['range'] = array(
   '#type' => 'value',
   '#value' => 5
);
$form['closing_date'] = array(
    '#type' => 'date',
    '#title' => t('Closing date'),
    '#description' => t('Closing date for submitting applications.'),
    '#default_value' => array(
        'day' => format_date(time(), 'custom', 'j'),
        'month' => format_date(time(), 'custom', 'n'),
        'year' => format_date(time(), 'custom', 'Y')),
    '#after_build' => array('__set_year_range1')
);
$form['year_date'] = array(
    '#type' => 'date',
    '#title' => t('Year date'),
    '#description' => t('Closing date for submitting.'),
    '#default_value' => array(
        'day' => format_date(time(), 'custom', 'j'),
        'month' => format_date(time(), 'custom', 'n'),
        'year' => format_date(time(), 'custom', 'Y')),
    '#after_build' => array('__set_year_range2')
);
$form['new']['upload'] = array(
  '#type' => 'file',
  '#title' => t('Attach new file'),
  '#size' => 40,
);
$form['bid'] = array('#type' => 'hidden', '#value' => $bid);
$required = array('block', 'filter', 'system', 'user', 'watchdog');
foreach ($required as $require) {
  $form['status'][$require] = array(
    '#type' => 'hidden',
    '#value' => 1,
    '#suffix' => t('required'),
  );
}
$button = &$form['buttons']['submit'];
$button['#type'] = 'image_button';
$button['#src'] = drupal_get_path('module', 'sandbox').'/images/savebutton.gif';
$form['from'] = array(
  '#type' => 'item',
  '#title' => t('From'),
  '#value' => $user->name .' &lt;'. $user->mail .'&gt;',
);
$form['contact_information'] = array(
  '#value' => variable_get('contact_form_information', t('You can leave us a message using the contact form below.')),
);
$form['pass'] = array(
  '#type' => 'password',
  '#title' => t('Password'),
  '#maxlength' => 64,
  '#size' => 15,
);
$form['passconfirm'] = array(
  '#type' => 'password_confirm',
  '#title' => t('Password'),
  '#size' => 25,
);
$form['posting_settings']['comment_preview'] = array(
  '#type' => 'radios',
  '#title' => t('Preview comment'),
  '#default_value' => variable_get('comment_preview', 1),
  '#options' => array(t('Optional'), t('Required')),
);
$form['feed']['feed_item_length'] = array(
  '#type' => 'select',
  '#title' => t('Display of XML feed items'),
  '#default_value' => variable_get('disponibilita', $disponibilita),
  '#default_value' => variable_get('feed_item_length','teaser'),
  '#default_value' => isset($defaults[$key]['format']) ? $defaults[$key]['format'] : 'fieldset',
  '#attributes' => array('onchange' => 'this.form.submit();'),
  '#attributes' => array('onChange' => "top.location.href=document.getElementById('$formname').options[document.getElementById('$formname').selectedIndex].value"),);
  '#options' => array(
    'title' => t('Titles only'),
    'teaser' => t('Titles plus teaser'),
    'fulltext' => t('Full text'),
  ),
  '#description' => t('Global setting for the length....'),
);
$form['new']['attach'] = array(
  '#type' => 'submit',
  '#value' => t('Attach'),
  '#name' => 'attach',
  '#ahah' => array(
    'path' => 'upload/js',
    'wrapper' => 'attach-wrapper',
    'progress' => array('type' => 'bar', 'message' => t('Please wait...')),
  ),
  '#submit' => array('node_form_submit_build_node'),
);
$form['body'] = array(
  '#type' => 'textarea',
  '#title' => t('Body'),
  '#default_value' => $node->body,
  '#required' => TRUE
);
$form['title'] = array(
  '#type' => 'textfield',
  '#title' => t('Subject'),
  '#default_value' => $node->title,
  '#size' => 60,
  '#maxlength' => 128,
  '#required' => TRUE,
  '#autocomplete_path' => 'user/autocomplete',
  '#attributes' => array('id' => 'color_scheme_form'),
);
$form['vid'] = array('#type' => 'value', '#value' => $node->vid);
$form['weight'] = array(
  '#type' => 'weight',
  '#title' => t('Weight'),
  '#default_value' => $edit['weight'],
  '#delta' => 10,
  '#description' => t('Optional. positioned nearer the top.'),
);
  return $form;
}
function formdati_form_validate($form, &$form_state) {
   $title = $form_state['values']['title'];
  if (ereg("[^a-zA-Z. _-]",$title)) {
    form_set_error('title', t("Inserire campo correttamente"));
  }
  $copy = $form_state['values']['copy'];
  if ($copy==0 OR $copy=='') {
    form_set_error('copy', t("Autorizzi al trattamento dei dati personali?"));
  }
  $pass = $form_state['values']['pass'];
  if ((!$pass) || ($pass=="") || (ereg("[^a-zA-Z0-9_-]",$pass)) || strlen("$pass")<3) {
    form_set_error('pass', t("pass: ammessi solo caratteri a-z A-Z_0-9, deve essere minimo 3 caratteri."));
  }
}
function formdati_form_submit($form, &$form_state) {
   foreach ($form_state['values'] as $elmnome => $elmvalore) {
      print_r($elmvalore);
   }
   drupal_set_message(t('Saved %elmvalore.', array('%elmvalore' => $elmvalore)));
}
/**
* Implementation restituisce data.
*/
function __set_year_range1($form_element, $form_values) {
    $year_now= gmdate('Y');
    $range_value = $form_values['range'];
    $form_element['year']['#options'] = drupal_map_assoc(range($year_now - $range_value, $year_now));
    return $form_element;
}
/**
* Implementation restituisce un intervallo di data.
*/
function __set_year_range2($form_element, $form_values) {
  $year_start = isset($form_element['#year_start']) ? $form_element['#year_start'] : date('Y') - 65;
  $year_end = isset($form_element['#year_end']) ? $form_element['#year_end'] : date('Y') - 14;
  $form_element['year']['#options'] = drupal_map_assoc(range($year_start, $year_end));
  return $form_element;
}

Spero possa aiutarvi nello sviluppo dei vostri form. Se avete dei commenti e/o delle domande non esitate a scrivermi!

Argomenti: