I am having difficulty implementing custom validation on a webform submission in Drupal 7 with the Webform module.

The code posted below is contained in a custom module which is activated when the submit button on the form is pressed. I am new to Drupal and php but my understanding is that this code should hook into the webform submission, validate the entries and allow me to manage the data submitted by the user. In the current configuration I would expect to see each of the test codes and the name entered on the form echo back. However, the only code, which returns, is test 4 at the bottom, no error is encountered and it appears not to evaluate any of the other code.

Does anyone have an idea where I am going wrong?

/**
* Implementation of hook_form_alter().
*/

function mywebform_extra_form_alter(&$form, &$form_state, $form_id) 
{
    // Add validation for a particular Webform node:
    if ($form_id == 'webform_client_form_8') 
    {
echo 'test 1';
        // Simply add the additional validate handler.
        $form['#validate'][] = 'mywebform_extra_validate_8';
    
        // Add the submit handler after the existing Webform submit handler,
        // but before the second Webform handler. Pop off the first one and add
        // ours second.
        $first = array_shift($form['#submit']);
        array_unshift($form['#submit'], $first, 'mywebform_extra_submit_8');
    }
}

/*
* Validation handler for Webform ID #8.
*/
//just make sure that a name was entered
function mywebform_extra_validate_8(&$form, &$form_state) 
{
echo 'test 2';
    if ($form_state['values']['name'] == '') 
    {
    form_set_error('', t('name must be entered.'));
    }
}

/**
* Submit handler for Webform ID #8.
*/
function mywebform_extra_submit_8(&$form, &$form_state) 
{


  // Changes can be made to the Webform node settings by modifying this variable:
  $form['#node']->webform;

    // Insert things into other database tables or modify properties.
    $name = $form_values['submitted_tree']['name'];
echo $name;	

}
echo 'test 4';

Comments

coreyp_1’s picture

First, clear your cache and see if Drupal picks up the hook then. Put a print statement inside of your hook_form_alter() to verify that it is working.

Second, print out the form ids to make sure that you are grabbing on to the correct one.

Everything else looks right.

Nemeton’s picture

Thanks for the help, clearing the cache did cause it to pick up the hook. Test codes indicate that the form id is correct and that the functions are being called as expected.

However, I now have a new problem, I do not seem to be able to actually read the data submitted. Execution gets as far as the validation function at which point $form_state['values']['name'] gives this error: Undefined index: name in mywebform_extra_validate_8().

This is my first time working with Drupal but my understanding is that $form_state['values']['name'] should return the value submitted to the form field labeled name but it returns the above error instead. Does anyone have an idea what I might be doing wrong?

coreyp_1’s picture

Unfortunately, modules may not always lay out their forms as directly as we would like. The easiest thing would be to just print out the contents of $form_state['values'] and look for it.

From a development standpoint, most of us will enable the Devel module and then do a dsm($form_state['values']); and look around at what is available. If it's a big variable, then consider using Search Krumo as well. Just don't leave them enabled on a live site.