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
Looked at Form Confirmation Logic in system.module
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:
In form:
In confirmation form:
In hook_submit:
I finally got something to work.
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:
In hook_menu I create callbacks for both the original form and the confirmation form:
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.
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.
You should use confirm_form
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'));
}