Custom coding: Adding advanced validation or submit code
An example for your particular use-case would look like the following:
- Make a new directory in sites/all/modules called "mywebform_extra".
- Make a new text file in sites/all/modules/mywebform_extra called mywebform_extra.info. Put this inside of it:
name = Webform Extra
description = Customizations for the Webform module.
core = 6.x
package = Webform
dependencies[] = webform
- Make a new text file in sites/all/modules/mywebform_extra called mywebform_extra.module. Put this inside of it:
/**
* 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_44') {
// Simply add the additional validate handler.
$form['#validate'][] = 'mywebform_extra_validate_44';
// 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_44');
}
}
/**
* Validation handler for Webform ID #44.
*/
function mywebform_extra_validate_44(&$form, &$form_state) {
global $user;
if (!isset($user->roles[4])) {
form_set_error('', t('Your user role is not allowed to submit this Webform.'));
}
}
/**
* Submit handler for Webform ID #44.
*/
function mywebform_extra_submit_44(&$form, &$form_state) {
global $user;
// 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.
}
- Note that "webform_client_form_44" means modify the Webform form for the node with NID "44". Adjust this to match whichever webform node's form you're modifying.
- Turn on the new Webform Extra module you've just created.
Besides this tutorial, there is also an excellent reference at http://www.drupalcoder.com/blog/additional-processing-in-drupals-webform...
Help improve this page
You can:
- Log in, click Edit, and edit this page
- Log in, click Discuss, update the Page status value, and suggest an improvement
- Log in and create a Documentation issue with your suggestion