Form 常用元素

1 篇文章 / 0 new
author
Form 常用元素
► Text Field
$form['pet_name'] = array(
    '#title' => t('Name'),
    '#type' => 'textfield',
    '#description' => t('Enter the name of your pet.'),
    '#default_value' => $user->pet_name,
    '#maxlength' => 32,
    '#required' => TRUE,
    '#size' => 15,
    '#weight' => 5,
    '#autocomplete_path' => 'pet/common_pet_names',
    );
 
  $form['pet_weight'] = array(
    '#title' => t('Weight'),
    '#type' => 'textfield',
    '#description' => t('Enter the weight of your pet in kilograms.'),
    '#field_suffix' => t('kilograms'),
    '#default_value' => $user->pet_weight,
    '#size' => 4,
    '#weight' => 10,
    );
► Password
$form['pass'] = array('#type' => 'password',
  '#title' => t('Password'),
  '#maxlength' => 60,
  '#size' => 15,
  '#required' => TRUE,
); 
► Password with Confirmation
$form['account']['pass'] = array(
  '#type' => 'password_confirm',
  '#description' => t('To change the current user password, enter the new
   password in both fields.'),
  '#size' => 25,
);
► Textarea
$form['pet_habits'] = array(
    '#title' => t('Habits'),
    '#type' => 'textarea',
    '#description' => t('Describe the habits of your pet.'),
    '#default_value' => $user->pet_habits,
    '#cols' => 40,
    '#rows' => 3,
    '#resizable' => FALSE,
    '#weight' => 15,
    );
► Select
$period = drupal_map_assoc(array(3600, 10800, 21600, 32400, 43200, 86400, 172800,
  259200, 604800, 1209600, 2419200, 4838400, 9676800), 'format_interval');
/* Period now looks like this:
  Array (
    [3600] => 1 hour
     [10800] => 3 hours
     [21600] => 6 hours
     [32400] => 9 hours
     [43200] => 12 hours
     [86400] => 1 day
     [172800] => 2 days
     [259200] => 3 days
     [604800] => 1 week
     [1209600] => 2 weeks
     [2419200] => 4 weeks
     [4838400] => 8 weeks
     [9676800] => 16 weeks )
*/
  $form['access']['statistics_flush_accesslog_timer'] = array(
    '#type' => 'select',
    '#title' => t('Discard access logs older than'),
    '#default_value' => variable_get('statistics_flush_accesslog_timer', 259200),
    '#options' => $period,
    '#description' => t('Older access log entries (including referrer statistics)
      will be automatically discarded. (Requires a correctly configured
      <a href="@cron">cron maintenance task</a>.)', array('@cron' =>
      url('admin/reports/status'))),
  );
//
$options = array(
    array(
      t('Healthy') => array(
        1 => t('wagging'),
        2 => t('upright'),
        3 => t('no tail')
      ),
    ),
    array(
      t('Unhealthy') => array(
        4 => t('bleeding'),
        5 => t('oozing'),
      ),
    ),
  );
  $form['pet_tail'] = array(
    '#title' => t('Tail demeanor'),
    '#type' => 'select',
    '#description' => t('Pick the closest match that describes the tail
      of your pet.'),
    '#options' => $options,
    '#multiple' => FALSE,
    '#weight' => 20,
  );
► Radio Buttons
$form['user_vis_settings']['custom'] = array(
  '#type' => 'radios',
  '#title' => t('Custom visibility settings'),
  '#options' => array(
    t('Users cannot control whether or not they see this block.'),
    t('Show this block by default, but let individual users hide it.'),
    t('Hide this block by default but let individual users show it.')
  ),
  '#description' => t('Allow individual users to customize the visibility of
    this block in their account settings.'),
  '#default_value' => $edit['custom'],
);
► Check Boxes
$options = array(
  'poison' => t('Sprays deadly poison'),
  'metal' => t('Can bite/claw through metal'),
  'deadly' => t('Killed previous owner') );
$form['danger'] = array(
  '#title' => t('Special conditions'),
  '#type' => 'checkboxes',
  '#description' => (t('Please note if any of these conditions apply to your
    pet.')),
  '#options' => $options,
  '#weight' => 25,
);
► Value
$form['pid'] = array(
  '#type' => 'value',
  '#value' => 123,
);
► Hidden
$form['my_hidden_field'] = array(
  '#type' => 'hidden',
  '#value' => t('I am a hidden field value'),
);
► Date
$form['deadline'] = array(
  '#title' => t('Deadline'),
  '#type' => 'date',
  '#description' => t('Set the deadline.'),
  '#default_value' => array(
    'month' => format_date(time(), 'custom', 'n'),
    'day' => format_date(time(), 'custom', 'j'),
    'year' => format_date(time(), 'custom', 'Y'),
  ),
);
► Weight
$form['weight'] = array(
  '#type' => 'weight',
  '#title' => t('Weight'),
  '#default_value' => 0,
  '#delta' => 10,
  '#description' => t('In listings, the heavier vocabularies will sink and the
    lighter vocabularies will be positioned nearer the top.'),
);
► File Upload
$form['picture']['picture_upload'] = array(
  '#type' => 'file',
  '#title' => t('Upload picture'),
  '#size' => 48,
  '#description' => t('Your virtual face or picture.')
);
► Fieldset
$form['author'] = array(
  '#type' => 'fieldset',
  '#access' => user_access('administer nodes'),
  '#title' => t('Authoring information'),
  '#collapsible' => TRUE,
  '#collapsed' => TRUE,
  '#weight' => 20,
);
► Submit
$form['submit'] = array(
  '#type' => 'submit',
  '#value' => t('Continue'),
); 
► Button / Image Button
$form['my_image_button'] = array(
  '#type' => 'image_button',
  '#src' => 'misc/powered-blue-80x15.png',
  '#value' => 'foo',
);
► Markup
$form['disclaimer'] = array(
  '#prefix' => '<div>',
  '#markup' => t('The information below is entirely optional.'),
  '#suffix' => '</div>',
);
► Item
$form['removed'] = array(
  '#title' => t('Shoe size'),
  '#type' => 'item',
  '#description' => t('This question has been removed because the law prohibits us
    from asking your shoe size.'),
);
Free Web Hosting