0

I've looked at a number of posts, but none of them have quite the same issue as I am. I've added two custom submit buttons to a webform (in addition to the default one created by the admin page.) My custom validate function and submit function are being called, but the default submit function is not - i.e. the error logging I've put in my custom function gets printed, but the form is NOT actually submitted. Any ideas?

function create_vector_user_form_alter(&$form, &$form_state, $form_id) {
  switch ($form_id) {
  case 'webform_client_form_24': // free trial webform
    $form['submit']['64_bit_btn'] = array(
      '#name' => 'op',
      //'#button_type' => 'submit',
      '#type'   => 'submit',
      '#value' => t('DOWNLOAD 64-BIT TRIAL'),
      '#executes_submit_callback' => TRUE,
    );

    $form['#validate'][] = 'create_vector_user_free_trial_form_validate';
    $form['#submit'][] = 'create_vector_user_free_trial_form_submit';
    $form['actions']['submit']['#submit'][] = 'create_vector_user_free_trial_form_submit';
    break;
  }
}

function create_vector_user_free_trial_form_validate($form, &$form_state) {
// this function runs correctly

}

    function create_vector_user_free_trial_form_submit($form_id, &$form_state) {
    error_log("submit!");
    if (strtoupper($form_state['clicked_button']['#value']) == 'DOWNLOAD 64-BIT TRIAL') {
        error_log("submitted 64");
    }
    else if(strtoupper($form_state['clicked_button']['#value']) == 'EMAIL ME A LINK') {
        error_log("submitted email");
      $to = $form_state['values']['submitted']['email']; 
      $params = array();
      $params['version'] = $form_state['values']['submitted']['download_type'];
      $key = 'homepage';

      drupal_mail('create_vector_user', $key, $to, language_default(), $params);
    }

    else {
        error_log("submitted default 32");
    }

  error_log("test end");

}

ETA: I've also tried this in my form_alter function based on this article about custom validation:

$form['#validate'][] = 'create_vector_user_free_trial_form_validate';
// $form['#submit'][] = 'create_vector_user_free_trial_form_submit';
$first = array_shift($form['#submit']);
array_unshift($form['#submit'], $first,'create_vector_user_free_trial_form_submit');

//$form['actions']['submit']['#submit'][] = 'create_vector_user_free_trial_form_submit';

6
  • Is all this code really needed to recreate your issue? Are you sure there aren't any parts you could simply remove, for sake of simplicity, and still have the behavior you want to ask about?
    – Mołot
    Commented Oct 1, 2015 at 22:42
  • I don't know! That's why I posted the question. I'm not super-familiar with module development or altering forms. I was following several examples I found online, including the link I posted. What parts do you think are unnecessary?
    – EmmyS
    Commented Oct 1, 2015 at 22:46
  • Your create_vector_user_free_trial_form_submit function doesn't kill the process of the submit? I mean, it doesn't have a die(), or a redirect? if you add a dd("test"); at the very end of your function this test string is printed in the log?
    – Gnuget
    Commented Oct 2, 2015 at 0:56
  • @Gnuget - I originally had the entire code posted, but someone indicated that I shouldn't post all my code. No, there's no die or redirect in my custom function. I've added the code back to the OP.
    – EmmyS
    Commented Oct 2, 2015 at 1:58
  • @Gnuget - I also added a test log at the very end of the submit function, outside the if/elseif/else conditional. It's hitting both the "submitted 64" error and the "test end" error. So I can't figure out why the form data isn't submitting.
    – EmmyS
    Commented Oct 2, 2015 at 2:09

1 Answer 1

2

I found the problem.

One of the features of webform is the ability of have multi-page forms, and the way how webform determine if it is in the "final" page is checking if the submit button has the value "Submit" and if that is not the case then it thinks to there is another page to evaluate before to submit the last function.

You can see this in the file webform.module in the line 3174:

 // Check for a multi-page form that is not yet complete.
 $submit_op = !empty($form['actions']['submit']['#value']) ? 
 $form['actions']['submit']['#value'] : t('Submit');
 $draft_op = !empty($form['actions']['draft']['#value']) ? 
 $form['actions']['draft']['#value'] : t('Save Draft');
if (!in_array($form_state['values']['op'], array($submit_op,   
$draft_op, '__AUTOSAVE__'))) {

In the case of your example $form_state['values']['op'] doesn't exists and even if it exists it wouldn't has the value 'Submit' so it will try to handle your form as a multipage form and It will not execute the last part of the function.

What I did to make your example work was only add the this in your submit function $form_state['values']['op'] = 'Submit'; and that's it.

It feels like a hack but that is the way how webform work :(

Below the example if you want to compare it with your code:

<?php

/**
 * Implements hook_form_alter().
 */
function test1_form_alter(&$form, &$form_state, $form_id)
{
    switch ($form_id) {
        case 'webform_client_form_5':
            $form['64_bit_btn'] = array(
                '#name' => 'op',
                '#type' => 'submit',
                '#value' => t('DOWNLOAD 64-BIT TRIAL'),
            );
            $form['#validate'][] = 'create_vector_user_free_trial_form_validate';
            array_unshift($form['#submit'], 'create_vector_user_free_trial_form_submit');
            break;
    }
}

function create_vector_user_free_trial_form_validate($form, &$form_state) {
    // this function runs correctly
}

function create_vector_user_free_trial_form_submit($form_id, &$form_state) {
     // add your code here
    $form_state['values']['op'] = 'Submit';
}

(please note to my module is called test1 you need change this with the name of your module)

7
  • So are you saying the ONLY thing that should be in my submit function is that one line, and my other submit code should go somewhere else? Or do I add that line to the existing submit code?
    – EmmyS
    Commented Oct 4, 2015 at 20:04
  • Add that line to your existing code, I added a comment in my submit function indicating where your code should go.
    – Gnuget
    Commented Oct 4, 2015 at 20:05
  • Thanks. There must be something else going on, then, because it still doesn't submit.
    – EmmyS
    Commented Oct 4, 2015 at 20:07
  • Try my example and check if I it is working and after that start adding your code in the submit function, It should work.
    – Gnuget
    Commented Oct 4, 2015 at 20:09
  • I've removed all my custom code from the submit function, and left just your line. Still doesn't submit.
    – EmmyS
    Commented Oct 4, 2015 at 20:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.