Drupal Technical
[SOLVED][Drupal Webform] How to add custom validation/submit code to a Webform?
A lot of Drupal users wanted to know how to add custom validation/submit code to a Webform. If you are faced with the same question with Webform in your Drupal site then read on to know the details.
- Use the following code in one of your custom modules or create one specifically for this purpose
<?php /** * 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_21') { // Simply add the additional validate handler. $form['#validate'][] = 'mywebform_extra_validate_21'; // 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_21'); } } /** * Validation handler for Webform ID #21. */ function mywebform_extra_validate_21(&$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 #21. */ function mywebform_extra_submit_21(&$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. } ?>
- Just remember that "webform_client_form_21" is an example id of the id of the webform. Replace it with the id of the webform you need to change.
Hope that helps.
The easiest way to solve a Drupal issue is to hand it to the Drupal experts. We can provide a wide range of Drupal services to help you maintain and manage your Drupal websites. Get in touch with us to know more.
Reference: http://drupal.org/node/1291574