You are here

function webform_validation_webform_validation_validate in Webform Validation 6

Same name and namespace in other branches
  1. 7 webform_validation.validators.inc \webform_validation_webform_validation_validate()

Implementation of hook_webform_validation_validate().

File

./webform_validation.validators.inc, line 260
Provides validation functionality and hooks

Code

function webform_validation_webform_validation_validate($validator_name, $items, $components, $rule) {

  /**
   * Preparation for select_* validation rules and the handling of key 0 for select components
   *
   * Only webform 3 handles a select list with key 0 properly.
   * The array_filter callback function is only loaded for webform 3 to make sure
   * it works perfectly for webform 3, and works in all cases except with key 0 for webform 2
   */
  $version = webform_validation_check_version();
  $check_false_callback = $version == 3 ? '_webform_validation_check_false' : NULL;
  if ($items) {
    $errors = array();
    switch ($validator_name) {
      case 'numeric':
        $num_range = _webform_numeric_check_data($rule['data']);
        foreach ($items as $key => $val) {
          if ($val != '') {

            // first check if the value is numeric
            if (!is_numeric($val)) {
              $errors[$key] = t('%item is not numeric', array(
                '%item' => $components[$key]['name'],
              ));
            }

            // now validate the entered numeric value against the validator range settings, if appropriate
            // a. validate min & max
            if (isset($num_range['min']) && isset($num_range['max'])) {

              // validate the min - max range
              if ($val < $num_range['min'] || $val > $num_range['max']) {
                $errors[$key] = t('%item is not within the allowed range %range', array(
                  '%item' => $components[$key]['name'],
                  '%range' => str_replace('|', ' - ', $rule['data']),
                ));
              }
            }
            else {

              // b. validate min
              if (isset($num_range['min'])) {
                if ($val < $num_range['min']) {
                  $errors[$key] = t('%item should be greater than or equal to %val', array(
                    '%item' => $components[$key]['name'],
                    '%val' => $num_range['min'],
                  ));
                }
              }

              // c. validate max
              if (isset($num_range['max'])) {
                if ($val > $num_range['max']) {
                  $errors[$key] = t('%item should be less than or equal to %val', array(
                    '%item' => $components[$key]['name'],
                    '%val' => $num_range['max'],
                  ));
                }
              }
            }
          }
        }
        return $errors;
        break;
      case 'min_length':
        $min_length = $rule['data'];
        foreach ($items as $key => $val) {
          if ($val != '' && drupal_strlen($val) < $min_length) {
            $errors[$key] = t('%item needs to be at least %num characters long', array(
              '%item' => $components[$key]['name'],
              '%num' => $min_length,
            ));
          }
        }
        return $errors;
        break;
      case 'max_length':
        $max_length = $rule['data'];
        foreach ($items as $key => $val) {
          if ($val != '' && drupal_strlen($val) > $max_length) {
            $errors[$key] = t('%item can be maximum %num characters long', array(
              '%item' => $components[$key]['name'],
              '%num' => $max_length,
            ));
          }
        }
        return $errors;
        break;
      case 'min_words':
        $min_words = $rule['data'];
        foreach ($items as $key => $val) {
          if ($val != '' && count(preg_split("/[\\s]+/", trim($val))) < $min_words) {
            $error = format_plural($min_words, '%item needs to be at least 1 word long', '%item needs to be at least @count words long', array(
              '%item' => $components[$key]['name'],
            ));
            $errors[$key] = $error;
          }
        }
        return $errors;
        break;
      case 'max_words':
        $max_words = $rule['data'];
        foreach ($items as $key => $val) {
          if ($val != '' && count(preg_split("/[\\s]+/", trim($val))) > $max_words) {
            $error = format_plural($max_words, '%item can be maximum 1 word long', '%item can be maximum @count words long', array(
              '%item' => $components[$key]['name'],
            ));
            $errors[$key] = $error;
          }
        }
        return $errors;
        break;
      case "equal":
        $first_entry_key = key($items);
        $first_entry = array_shift($items);
        $first_entry = _webform_validation_flatten_array($first_entry);

        // flatten in case of array
        // now check if following components equal the first one
        foreach ($items as $key => $val) {
          $val = _webform_validation_flatten_array($val);

          // flatten in case of array
          if ($val !== $first_entry) {
            $errors[$key] = t('%item_checked does not match %item_first', array(
              '%item_checked' => $components[$key]['name'],
              '%item_first' => $components[$first_entry_key]['name'],
            ));
          }
        }
        return $errors;
        break;
      case "unique":
        foreach ($items as $key => $val) {
          if (is_array($val)) {

            // make sure to flatten arrays first
            $items[$key] = _webform_validation_flatten_array($val);
          }
          if (empty($items[$key])) {

            // items without a value selected shouldn't be validated
            unset($items[$key]);
          }
        }

        // now we count how many times each value appears, and find out which values appear more than once
        $items_count = array_count_values(array_map('strtolower', array_map('trim', $items)));
        $doubles = array_filter($items_count, create_function('$x', 'return $x > 1;'));
        foreach ($items as $key => $val) {
          if (in_array(strtolower($val), array_keys($doubles))) {
            $errors[$key] = t('The value of %item is not unique', array(
              '%item' => $components[$key]['name'],
            ));
          }
        }
        return $errors;
        break;
      case "specific_value":
        $specific_values = explode(',', $rule['data']);
        $specific_values = array_map('trim', $specific_values);
        foreach ($items as $key => $val) {
          if (is_array($val)) {
            $val = _webform_validation_flatten_array($val);
          }
          if (!in_array($val, $specific_values)) {
            $errors[$key] = _webform_validation_i18n_error_message($rule);
          }
        }
        return $errors;
        break;
      case "not_default_value":
        foreach ($items as $key => $val) {
          if (is_array($val)) {
            $val = _webform_validation_flatten_array($val);
          }
          if ($val == $components[$key]['value']) {
            $errors[$key] = _webform_validation_i18n_error_message($rule);
          }
        }
        return $errors;
        break;
      case "oneoftwo":

        // $components should have 2 items
        $keys = array_keys($items);
        $item1 = array_shift($keys);
        $item2 = array_shift($keys);
        $entry1 = _webform_validation_flatten_array($items[$item1]);
        $entry2 = _webform_validation_flatten_array($items[$item2]);
        if (empty($entry1) && empty($entry2)) {
          return array(
            $item1 => t('You have to specify %item1 or %item2 (or both)', array(
              '%item1' => $components[$item1]['name'],
              '%item2' => $components[$item2]['name'],
            )),
          );
        }
        break;
      case "oneofseveral":
        foreach ($items as $key => $val) {
          if (is_array($val)) {

            // make sure to flatten arrays first
            $items[$key] = _webform_validation_flatten_array($val);
          }
        }

        // $components should have at least one of several items
        if (count(array_filter($items)) < 1) {
          $keys = array_keys($items);
          $names = array();
          foreach ($keys as $value) {
            $names[] = _webform_filter_xss($components[$value]['name']);
          }
          return array(
            $keys[0] => t('You have to specify at least one of these items:') . theme('item_list', $names),
          );
        }
        break;
      case "select_min":
        $min_selections = $rule['data'];
        foreach ($items as $key => $val) {
          $val = is_array($val) ? $val : array(
            $val,
          );

          // workaround for single select components
          $selected_values = isset($check_false_callback) ? array_filter($val, $check_false_callback) : array_filter($val);
          if (is_array($val) && count($selected_values) < $min_selections) {
            $errors[$key] = t('Please select at least %num options for %item', array(
              '%num' => $min_selections,
              '%item' => $components[$key]['name'],
            ));
          }
        }
        return $errors;
        break;
      case "select_max":
        $max_selections = $rule['data'];
        foreach ($items as $key => $val) {
          $val = is_array($val) ? $val : array(
            $val,
          );

          // workaround for single select components
          $selected_values = isset($check_false_callback) ? array_filter($val, $check_false_callback) : array_filter($val);
          if (is_array($val) && count($selected_values) > $max_selections) {
            $errors[$key] = t('Please select maximum %num options for %item', array(
              '%num' => $max_selections,
              '%item' => $components[$key]['name'],
            ));
          }
        }
        return $errors;
        break;
      case "select_exact":
        $allowed_selections = $rule['data'];
        foreach ($items as $key => $val) {
          $selected_values = isset($check_false_callback) ? array_filter($val, $check_false_callback) : array_filter($val);
          if (is_array($val) && count($selected_values) != $allowed_selections) {
            $errors[$key] = t('Please select %num options for %item', array(
              '%num' => $allowed_selections,
              '%item' => $components[$key]['name'],
            ));
          }
        }
        return $errors;
        break;
      case "plain_text":
        foreach ($items as $key => $val) {
          if ($val != '' && strcmp($val, strip_tags($val))) {
            $errors[$key] = t('%item only allows the use of plain text', array(
              '%item' => $components[$key]['name'],
            ));
          }
        }
        return $errors;
        break;
      case "regex":
        mb_regex_encoding('UTF-8');
        $regex = $rule['data'];
        foreach ($items as $key => $val) {
          if ($val != '' && !mb_ereg("{$regex}", $val)) {
            $errors[$key] = _webform_validation_i18n_error_message($rule);
          }
        }
        return $errors;
        break;
      case 'must_be_empty':
        foreach ($items as $key => $val) {
          if ($val) {
            $errors[$key] = t('%item does not contain the correct data', array(
              '%item' => $components[$key]['name'],
            ));
          }
        }
        return $errors;
        break;
      case "blacklist":
        $blacklist = explode(',', $rule['data']);
        $blacklist = array_map('trim', $blacklist);
        $blacklist_regex = implode('|', $blacklist);
        foreach ($items as $key => $val) {
          if ($val != '' && preg_match("/{$blacklist_regex}/i", $val)) {
            $errors[$key] = _webform_validation_i18n_error_message($rule);
          }
        }
        return $errors;
        break;
      case "username":
        foreach ($items as $key => $val) {

          // load user - if username does not match or status 0 throw error
          if ($val != '') {
            if (!db_result(db_query("SELECT COUNT(*) FROM {users} WHERE name = '%s' AND status = 1", $val))) {

              // Username doesn't exist
              $errors[$key] = t('The %item field does not match an active username.', array(
                '%item' => $components[$key]['name'],
              ));
            }
          }
        }
        return $errors;
        break;
    }
  }
}