You are here

function webform_validate in Webform 5

Same name and namespace in other branches
  1. 5.2 webform.module \webform_validate()
  2. 6.2 webform.module \webform_validate()

Implementation of hook_validate(). This function is called after the user clicks any button displayed in webform_form(). Rather than a typical usage of validation, in webform this is used to perform various actions without kicking the user out of the 'edit' tab. For instance, webform detects the when the buttons "Add" or "Edit Selected" are clicked, and then direct the user to a form for editing a component.

File

./webform.module, line 486

Code

function webform_validate(&$node) {
  $op = $_POST['op'];
  $node->webformcomponents = _webform_components_decode($_POST['webformcomponents']);
  $node->selected_component = $_POST['components']['selected_component'];

  // Update the component weight and mandatory status.
  if ($op !== t('Done')) {
    foreach ($node->webformcomponents as $cid => $component) {
      $post_component = $_POST['components'][$cid];
      $node->webformcomponents[$cid]['weight'] = $post_component['weight'];
      $node->webformcomponents[$cid]['mandatory'] = isset($post_component['mandatory']) ? 1 : 0;
    }
  }
  switch ($op) {

    // Intercept these buttons and redirect:
    case t('Add'):
      $errors = form_get_errors();
      if (empty($errors)) {
        $output = drupal_get_form('webform_edit_field_form', $node);
        print theme('page', $output);
        exit;
      }
      break;
    case t('Edit Selected'):
      $errors = form_get_errors();
      if (empty($errors)) {
        $output = drupal_get_form('webform_edit_field_form', $node);
        print theme('page', $output);
        exit;
      }
      break;
    case t('Delete Selected'):

      // We have to throw a form error for Drupal to stop form processing.
      // The 'selected_component' field item is a radio button, which shouldn't
      // show a red border of any sort when an error is flagged on it.
      if ($_POST['components']['selected_component']) {
        form_set_error('selected_component', t("Field deleted, form must be submitted to save changes"));
      }
      else {
        form_set_error('selected_component', t("A component must be selected to delete"));
      }
      break;

    // Standard form checking:
    case t('Submit'):
    case t('Preview'):

      // Make sure the submission limiter is correctly set.
      if ($_POST['enforce_limit'] === 'yes') {
        if (!is_numeric($_POST['submit_limit'])) {
          form_set_error('submit_limit', t('Submission limit must be a number'));
        }
      }
      break;
  }
}