Creating a custom form from a module in Drupal 6


Permissions

This will check if the user has permissions to view the form

function myform_perm() {
  return array('access myform content');
}

Path

What url will lead us to the form, useful to start it with ‘admin’ so non-admin users can’t access it.

function myform_menu() {
  $items = array();
  $items['path/to/my/form'] = array(
    'title' => t('My Form'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('myform_form'),
    'access arguments' => array('access myform content'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}

Form

function myform_form() {
  $form['message'] = array(
    '#type' => 'textarea',
    '#title' => t('Message'),
    '#required' => TRUE,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Send')
  );
  return $form;
}

 Validation

function myForm_validate($node, &$form) {
  if (isset($node->end) && isset($node->start)) {
    if ($node->start > $node->end) {
      form_set_error('time', t('An event may not end before it starts.'));
    }
  }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s