I just can't seem to find any examples on how to confirm a form submission before submitting it. I found how to call a confirm screen, but not how to call the confirm screen after you have submitted the form.

Here's what I want to do, step by step:

1. Display form.
2. Click on "submit" button for form.
3. "Confirm" screen appears.
4. Click on "Process" button on confirm screen, and form is submitted for processing.

Does anyone know how this is done?

Here's my sample code:

In hook_menu, I have menu item to display the form (1):

    $items[] = array(
      'path' => 'admin/settings/lesspaper/batch_og',
      'title' => t('Batch Group Roles Processing'),
      'description' => t('Process batch group role changes.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('lesspaper_batch_og'),
      'access' => user_access('administer lesspaper'),
      'weight' => 10,
      'type' => MENU_LOCAL_TASK,
     );  

The form itself will display with a submit button. What I can't figure out is how to get it to go to "Confirm" screen when "submit" is clicked. Right now, of course, it simply processes through hook_submit when "submit" is clicked.

/**
 * Special lesspaper batch group role processing
 *
 */
 function lesspaper_batch_og() {

  drupal_set_title(t('Batch Group Processing'));

  $form_id = 'lesspaper_batch_og';
  $form = array();

  $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));

  return $form;

}

/**
 * Menu callback: submit processing of batch group roles request.
 */
function lesspaper_batch_og_submit($form_id, &$form) {
  drupal_set_message(t('Form submitted'));
}

Confirmation code I'd like to add:

/**
 * Menu callback: confirm processing of batch group roles request.
 */
function lesspaper_batch_og_confirm($form_id, &$form) {
  $question = "Confirm processing of this form?";
  $path = 'admin/settings/lesspaper/batch_og';
  $description = 'This action cannot be easily undone.';
  $yes = 'Process';
  $no = 'Cancel';
  return confirm_form($form, $question, $path, $description, $yes, $no = NULL, $name = 'confirm');

}

Comments

somebodysysop’s picture

I tried to follow the logic for form confirmation I see in the system.module. I have callback to form. Form calls confirmation_form, but form_values are not transferred so I never see the confirmation form. What am I doing wrong?

In hook_menu:

    $items[] = array(
      'path' => 'admin/settings/lesspaper/batch_og',
      'title' => t('Batch Group Roles Processing'),
      'description' => t('Process batch group role changes.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('lesspaper_batch_og'),
      'access' => user_access('administer lesspaper'),
      'weight' => 10,
      'type' => MENU_LOCAL_TASK,
     );  
    $items[] = array(
      'path' => 'admin/settings/lesspaper/batch_og/confirm',
      'title' => t('Batch Group Roles Processing'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('lesspaper_batch_og'),
      'access' => user_access('administer lesspaper'),
      'type' => MENU_CALLBACK
     );

In form:

 function lesspaper_batch_og($form_values = array()) {

  drupal_set_title(t('Batch Group Processing'));

  drupal_set_message('In lesspaper_batch_og ' . $form_values['go']);

  // Display the confirm form if any modules have been submitted.
  if ($confirm_form = lesspaper_batch_og_confirm($form_values)) {
    return $confirm_form;
  }

  $form_id = 'lesspaper_batch_og';

  $form = array();

  $form['go'] = array(
    '#type' => 'checkbox',
    '#title' => t('Go')
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Continue'),
  );

  $form['#multistep'] = TRUE;
  $form['#action'] = url('admin/settings/lesspaper/batch_og/confirm');

  return $form;

}

In confirmation form:

/**
 * Menu callback: confirm processing of batch group roles request.
 */
function lesspaper_batch_og_confirm($form) {

  drupal_set_message('In lesspaper_batch_og_confirm ' . $form['go']);

  // Nothing to build.
  if (!isset($form['go'])) {
    return;
  }

  $form['#tree'] = TRUE;
  $form['#multistep'] = TRUE;

  $question = "Confirm processing of this form?";
  $path = 'admin/settings/lesspaper/batch_og';
  $description = 'This action cannot be easily undone.';
  $yes = 'Process';
  $no = 'Cancel';
  
  $form = confirm_form($form, $question, $path, $description, $yes, $no = NULL, $name = 'confirm');
  return $form;
}

In hook_submit:

/**
 * Menu callback: submit processing of batch group roles request.
 */
function lesspaper_batch_og_submit($form_id, &$form) {
  drupal_set_message(t('Form submitted' . $form['go']));
}
somebodysysop’s picture

It's not what it should be, but after 4 days of banging my head against the wall with no help, I at least figured out how to get some sort of form confirmation working. For others who get as lost as me with no roadmap out, here's what I did:

I create two forms: an original and a confirmation. My flow is as follows:

original form -> original form submit -> confirmation form -> confirmation form submit

In hook_menu I create callbacks for both the original form and the confirmation form:

    // original form
    $items[] = array(
      'path' => 'admin/settings/lesspaper/batch_og',
      'title' => t('Batch Group Roles Processing'),
      'description' => t('Process batch group role changes.'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('lesspaper_batch_og'),
      'access' => user_access('administer lesspaper'),
      'weight' => 10,
      'type' => MENU_LOCAL_TASK,
     );  
    // confirmation form
    $items[] = array(
      'path' => 'admin/settings/lesspaper/batch_og/confirm',
      'title' => t('Batch Group Roles Processing'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('lesspaper_batch_og_confirm'),
      'access' => user_access('administer lesspaper'),
      'type' => MENU_CALLBACK
     );

In the original form note that we include an #action form element that contains the URL to the confirmation form. So when the "submit" button is pressed, the confirmation form is then called. Note that there's nothing in the original form submit hook except a message to the end user.

/**
 * Special lesspaper batch group role processing
 *
 */
 function lesspaper_batch_og() {

  drupal_set_title(t('Batch Group Processing'));

  drupal_set_message('In lesspaper_batch_og ');

  $form_id = 'lesspaper_batch_og';

  $form = array();

  $form['#multistep'] = TRUE;
  $form['#action'] = url('admin/settings/lesspaper/batch_og/confirm');

  $form['go'] = array(
    '#type' => 'checkbox',
    '#title' => t('Go')
  );

  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Continue'),
  );

  return $form;

}

/**
 * Menu callback: submit processing of batch group roles request.
 */
function lesspaper_batch_og_submit($form_id, &$form) {
  drupal_set_message(t('Verify submission.'));
}

In the confirmation form, we get the submitted form values (submitted from the original form) from $_POST. We then put those values into the new form however we want. We create the new form using a call to the confirm_form() function. It is in the confirmation form submit hook that we actually process the form input.

/**
 * Menu callback: confirm processing of batch group roles request.
 */
function lesspaper_batch_og_confirm() {

  // Get the post values (what user entered)
  $edit = $_POST;

  $form = array();

  $form['go'] = array(
    '#type' => 'checkbox',
    '#title' => t('Go'),
	'#value' => $edit['go'] // Include the $_POST form values
  );

  // Build the confirmation form

  $question = "Confirm processing of this form?";
  $path = 'admin/settings/lesspaper/batch_og';
  $description = 'This action cannot be easily undone.';
  $yes = 'Process';
  $no = 'Cancel';
  $name = 'confirm';
  
  // Display form confirmation
  $form = confirm_form($form, $question, $path, $description, $yes, $no, $name);

  return $form;
}

/**
 * Menu callback: submit processing of batch group roles request.
 */
function lesspaper_batch_og_confirm_submit($form_id, &$form) {
  drupal_set_message(t('Batch OG Request Processed'));
  // Your form processing code HERE
  return 'admin/settings/lesspaper/batch_og';
}
light-blue’s picture

You should use confirm_form instead. (http://api.drupal.org/api/function/confirm_form/5)

NOTE: "You should never directly inspect $_POST to see if an action was confirmed."

EXAMPLE: From contemplate...

function contemplate_delete_type_form($type = NULL){
$form['type'] = array('#type' => 'value', '#value' => $type);

return confirm_form($form,
t('Are you sure you want to delete the template for %type?', array('%type' => $type)),
isset($_GET['destination']) ? $_GET['destination'] : 'admin/content/templates',
t('This action cannot be undone.'),
t('Delete'), t('Cancel'));
}