Project

General

Profile

Revision 76bdcd04

Added by Assos Assos almost 6 years ago

Weekly update of contrib modules

View differences:

drupal7/sites/all/modules/webform_validation/webform_validation.validators.inc
8 8
/**
9 9
 * Implements hook_webform_validation_validators().
10 10
 *
11
 * This function returns an array of validators, in the validator key => options array form.
12
 * Possible options:
13
 * - name (required): name of the validator
14
 * - component types (required): defines which component types can be validated by this validator. Specify 'all' to allow all types
15
 * - negatable (optional): define whether the rule can be negated, meaning it will validate the inverse of the rule.
16
 * - custom_error (optional): define whether a user can specify a custom error message upon creating the validation rule.
17
 * - custom_data (optional): define whether custom data can be added to the validation rule
18
 * - min_components (optional): define the minimum number of components to be selected for creating a validation rule
19
 * - max_components (optional): define the maximum number of components to be selected for creating a validation rule
20
 * - description (optional): provide a descriptive explanation about the validator.
11
 * This function returns an array of validators, in the validator key => options
12
 * array form. Possible options:
13
 * - name (required): name of the validator.
14
 * - component_types (required): defines which component types can be validated
15
 *   by this validator. Specify 'all' to allow all types.
16
 * - negatable (optional): define whether the rule can be negated, meaning it
17
 *   will validate the inverse of the rule.
18
 * - custom_error (optional): define whether a user can specify a custom error
19
 *   message upon creating the validation rule.
20
 * - custom_data (optional): define whether custom data can be added to the
21
 *   validation rule.
22
 * - min_components (optional): define the minimum number of components to be
23
 *   selected for creating a validation rule.
24
 * - max_components (optional): define the maximum number of components to be
25
 *   selected for creating a validation rule.
26
 * - description (optional): provide a descriptive explanation about the
27
 *   validator.
21 28
 */
22 29
function webform_validation_webform_validation_validators() {
23 30
  $validators = array(
......
30 37
      ),
31 38
      'custom_data' => array(
32 39
        'label' => t('Specify numeric validation range'),
33
        'description' => t('Optionally specify the minimum-maximum range to validate the user-entered numeric value against.') . ' ' . t('Usage') . ':' . theme('item_list', array('items' => array(t('empty: no value validation'), t('"100": greater than or equal to 100'), t('"|100": less than or equal to 100 (including negative numbers)'), t('"0|100": greater than or equal to 0 & less than or equal to 100'), t('"10|100": greater than or equal to 10 & less than or equal to 100'), t('"-100|-10": greater than or equal to -100 & less than or equal to -10')))),
40
        'description' => t('Optionally specify the minimum-maximum range to validate the user-entered numeric value against. Usage:') . theme('item_list', array('items' => array(t('empty: no value validation'), t('"100": greater than or equal to 100'), t('"|100": less than or equal to 100 (including negative numbers)'), t('"0|100": greater than or equal to 0 & less than or equal to 100'), t('"10|100": greater than or equal to 10 & less than or equal to 100'), t('"-100|-10": greater than or equal to -100 & less than or equal to -10')))),
34 41
        'required' => FALSE,
35 42
      ),
36 43
      'description' => t('Verifies that user-entered values are numeric, with the option to specify min and / or max values.'),
......
439 446
    }
440 447
  }
441 448

  
442
  // Some components, such as multiple select, send their values as arrays, others don't.
443
  // Convert all to arrays and write the rules to handle them that way. Remove empty values.
449
  // Some components, such as multiple select, send their values as arrays,
450
  // others don't. Convert all to arrays and write the rules to handle them that
451
  // way. Remove empty values.
444 452
  foreach ($items as $key => $val) {
445 453
    $new_values = array();
446 454
    foreach ((array) $val as $val_key => $val_val) {
......
468 476
            continue;
469 477
          }
470 478

  
471
          // Now validate the entered numeric value against the validator range settings, if appropriate
479
          // Now validate the entered numeric value against the validator range
480
          // settings, if appropriate.
472 481
          // a. validate min & max.
473 482
          if (isset($num_range['min']) && isset($num_range['max'])) {
474 483
            // Validate the min - max range.
......
673 682
      foreach ($items as $key => $val) {
674 683
        $items[$key] = _webform_validation_flatten_array($val);
675 684
      }
676
      // Now we count how many times each value appears, and find out which values appear more than once.
685
      // Now we count how many times each value appears, and find out which
686
      // values appear more than once.
677 687
      $items_count = array_count_values(array_map('drupal_strtolower', array_map('trim', $items)));
678
      $doubles = array_filter($items_count, create_function('$x', 'return $x > 1;'));
688
      $doubles = array_filter($items_count, function ($x) {
689
        return $x > 1;
690
      });
679 691
      foreach ($items as $key => $val) {
680 692
        if (in_array(drupal_strtolower($val), array_keys($doubles))) {
681 693
          $errors[$key] = t('The value of %item is not unique.', array('%item' => $components[$key]['name']));
......
687 699
      $specific_values = explode(',', $rule['data']);
688 700
      $specific_values = array_map('trim', $specific_values);
689 701
      foreach ($items as $key => $val) {
690
        // Selects: Fail if at least one checked and at least one not in the allowed values.
702
        // Selects: Fail if at least one checked and at least one not in the
703
        // allowed values.
691 704
        $val = array_filter($val);
692 705
        $test = count($val) && count(array_diff($val, $specific_values));
693 706
        _webform_validation_test($errors, $key, $rule, $test);
......
930 943
}
931 944

  
932 945
/**
933
 * Helper function to negate validation rules as needed and create the correct error message.
946
 * Helper function to negate validation rules as needed.
947
 *
948
 * Creates the correct error message.
934 949
 */
935 950
function _webform_validation_test(&$errors, $key, $rule, $test, array $error_strings = NULL, array $error_vars = NULL) {
936 951
  $rule['negate'] = !empty($rule['negate']);
......
998 1013
  }
999 1014

  
1000 1015
  $validators = module_invoke_all('webform_validation_validators');
1001
  // Let modules use hook_webform_validator_alter($validators) to change validator settings.
1016
  // Let modules use hook_webform_validator_alter($validators) to change
1017
  // validator settings.
1002 1018
  drupal_alter('webform_validator', $validators);
1003 1019

  
1004 1020
  // Remove entries for which only partial information exists.
......
1090 1106
}
1091 1107

  
1092 1108
/**
1093
 * Process the numeric value validation range that was provided in the numeric validator options.
1109
 * Helper function to check numeric data.
1110
 *
1111
 * Process the numeric value validation range that was provided in the numeric
1112
 * validator options.
1094 1113
 */
1095 1114
function _webform_numeric_check_data($data) {
1096 1115
  $range = array('min' => NULL, 'max' => NULL);

Also available in: Unified diff