Projet

Général

Profil

Révision 81b16cc2

Ajouté par Assos Assos il y a plus de 6 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/webform_validation/webform_validation.validators.inc
2 2

  
3 3
/**
4 4
 * @file
5
 * Provides validation functionality and hooks
5
 * Provides validation functionality and hooks.
6 6
 */
7 7

  
8 8
/**
......
17 17
 * - custom_data (optional): define whether custom data can be added to the validation rule
18 18
 * - min_components (optional): define the minimum number of components to be selected for creating a validation rule
19 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
20
 * - description (optional): provide a descriptive explanation about the validator.
21 21
 */
22 22
function webform_validation_webform_validation_validators() {
23 23
  $validators = array(
......
30 30
      ),
31 31
      'custom_data' => array(
32 32
        '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') . ':'
34
          . 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')))),
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')))),
35 34
        'required' => FALSE,
36 35
      ),
37 36
      'description' => t('Verifies that user-entered values are numeric, with the option to specify min and / or max values.'),
......
102 101
      ),
103 102
      'custom_data' => array(
104 103
        'label' => t('Number the fields add up to'),
105
        'description' => t('Specify the number and the type of comparison. For example:') . theme('item_list', array('items' => array(
106
          t('Enter "=3" if the components must add up to exactly 3.'),
107
          t('Enter ">10" if the components must add up to greater than 10.'),
108
          t('Enter ">=10" if the components must add up to greater than or equal to 10.'),
109
          t('Enter "<20" if the components must add up to less than 20.'),
110
          t('Enter "<=20" if the components must add up to less than or equal to 20.'),
111
        ))),
104
        'description' => t('Specify the number and the type of comparison. For example:') . theme('item_list', array(
105
          'items' => array(
106
            t('Enter "=3" if the components must add up to exactly 3.'),
107
            t('Enter ">10" if the components must add up to greater than 10.'),
108
            t('Enter ">=10" if the components must add up to greater than or equal to 10.'),
109
            t('Enter "<20" if the components must add up to less than 20.'),
110
            t('Enter "<=20" if the components must add up to less than or equal to 20.'),
111
          ),
112
        )),
112 113
      ),
113 114
      'description' => t('Require the values of the selected fields to add up to exactly, greater than or equal to, or less than or equal to a specified number.'),
114 115
    ),
......
182 183
        'label' => t('(Key) value'),
183 184
        'description' => t('Specify the specific value(s) you want the component to contain. Separate multiple options by a comma. For components that have keys, use the key value instead.'),
184 185
      ),
185
      'max_components' => 1,
186 186
      'description' => t('Verifies that the value of the specified component is from a list of allowed values.'),
187 187
    ),
188 188
    'default_value' => array(
......
215 215
      ),
216 216
      'custom_data' => array(
217 217
        'label' => t('Number to be completed'),
218
        'description' => t('Specify the number that must be completed and the type of comparison. For example:') . theme('item_list', array('items' => array(
219
          t('Enter ">=1" if the user must complete <b>at least</b> 1 of the selected components.'),
220
          t('Enter "=3" if the user must complete <b>exactly</b> 3 of the selected components.'),
221
          t('Enter "<=2" if the user must complete <b>at most</b> 2 of the selected components.'),
222
        ))),
218
        'description' => t('Specify the number that must be completed and the type of comparison. For example:') . theme('item_list', array(
219
          'items' => array(
220
            t('Enter ">=1" if the user must complete <b>at least</b> 1 of the selected components.'),
221
            t('Enter "=3" if the user must complete <b>exactly</b> 3 of the selected components.'),
222
            t('Enter "<=2" if the user must complete <b>at most</b> 2 of the selected components.'),
223
          ),
224
        )),
223 225
      ),
224 226
      'min_components' => 2,
225 227
      'description' => t('Requires the user to complete some number of components out of a group of components. For example, complete at least 2 out of 3, complete at most 4 out of 6, or complete exactly 3 our of 4.'),
......
267 269
        'textarea',
268 270
        'textfield',
269 271
      ),
270
      'description' => t('Verifies that user-entered data doesn\'t contain HTML tags.'),
272
      'description' => t("Verifies that user-entered data doesn't contain HTML tags."),
271 273
    ),
272 274
    'starts_with' => array(
273 275
      'name' => t('Starts with'),
......
311 313
      'custom_error' => TRUE,
312 314
      'custom_data' => array(
313 315
        'label' => t('Pattern'),
314
        'description' => t('Specify a pattern where:')
315
          . theme('item_list', array('items' => array(t('@ = any letter A-Z'), t('# = any numeral 0-9'), t('| = separates two or more acceptable patterns'), t('Any other character must appear in its exact position'))))
316
          . t('Examples')
317
          . theme('item_list', array('items' => array(t('North American phone number: (###) ###-####'), t('D-2500 series model numbers: D-25##'), t('UK Postal Code: @# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA')))),
316
        'description' => t('Specify a pattern where:') . theme('item_list', array('items' => array(t('@ = any letter A-Z'), t('# = any numeral 0-9'), t('| = separates two or more acceptable patterns'), t('Any other character must appear in its exact position')))) . t('Examples') . theme('item_list', array('items' => array(t('North American phone number: (###) ###-####'), t('D-2500 series model numbers: D-25##'), t('UK Postal Code: @# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA')))),
318 317
      ),
319 318
      'description' => t('Verifies that a user-entered value follows to a specified pattern.'),
320 319
    ),
......
376 375
        'label' => t('Blacklisted words'),
377 376
        'description' => t('Specify illegal words, seperated by commas.'),
378 377
      ),
379
      'description' => t('Validates that user-entered data doesn\'t contain any of the specified illegal words.'),
378
      'description' => t("Validates that user-entered data doesn't contain any of the specified illegal words."),
380 379
    ),
381 380
    'username' => array(
382 381
      'name' => t('Must match a username'),
......
460 459
      $num_range = _webform_numeric_check_data($rule['data']);
461 460
      foreach ($items as $key => $val) {
462 461
        foreach ($val as $subval) {
463
          // first check if the value is numeric
462
          // First check if the value is numeric.
464 463
          if (is_numeric($subval)) {
465 464
            $subval = (float) $subval;
466 465
          }
......
469 468
            continue;
470 469
          }
471 470

  
472
          // now validate the entered numeric value against the validator range settings, if appropriate
473
          // a. validate min & max
471
          // Now validate the entered numeric value against the validator range settings, if appropriate
472
          // a. validate min & max.
474 473
          if (isset($num_range['min']) && isset($num_range['max'])) {
475
            // validate the min - max range
474
            // Validate the min - max range.
476 475
            if ($subval < $num_range['min'] || $subval > $num_range['max']) {
477
              $errors[$key] = t('%item is not within the allowed range %range.', array('%item' => $components[$key]['name'], '%range' => str_replace('|' , ' - ', $rule['data'])));
476
              $errors[$key] = t('%item is not within the allowed range %range.', array('%item' => $components[$key]['name'], '%range' => str_replace('|', ' - ', $rule['data'])));
478 477
            }
479 478
          }
480 479
          else {
481
            // b. validate min
480
            // b. validate min.
482 481
            if (isset($num_range['min'])) {
483 482
              if ($subval < $num_range['min']) {
484 483
                $errors[$key] = t('%item should be greater than or equal to %val.', array('%item' => $components[$key]['name'], '%val' => $num_range['min']));
485 484
              }
486 485
            }
487
            // c. validate max
486
            // c. validate max.
488 487
            if (isset($num_range['max'])) {
489 488
              if ($subval > $num_range['max']) {
490 489
                $errors[$key] = t('%item should be less than or equal to %val.', array('%item' => $components[$key]['name'], '%val' => $num_range['max']));
......
494 493
        }
495 494
      }
496 495
      return $errors;
496

  
497 497
    case 'min_length':
498 498
      $min_length = $rule['data'];
499 499
      foreach ($items as $key => $val) {
......
504 504
        }
505 505
      }
506 506
      return $errors;
507

  
507 508
    case 'max_length':
508 509
      $max_length = $rule['data'];
509 510
      foreach ($items as $key => $val) {
......
514 515
        }
515 516
      }
516 517
      return $errors;
518

  
517 519
    case 'min_words':
518 520
      $min_words = $rule['data'];
519 521
      foreach ($items as $key => $val) {
......
525 527
        }
526 528
      }
527 529
      return $errors;
530

  
528 531
    case 'max_words':
529 532
      $max_words = $rule['data'];
530 533
      foreach ($items as $key => $val) {
......
536 539
        }
537 540
      }
538 541
      return $errors;
542

  
539 543
    case 'sum':
540 544
      // Get the numbers to sum.
541 545
      $sum = array();
......
587 591
        $errors[$keys[0]] = t('These items must add up to %verb %compare_number:', array('%verb' => $error, '%compare_number' => $compare_number)) . theme('item_list', array('items' => _webform_validation_get_names_of_rule_components($rule)));
588 592
      }
589 593
      return $errors;
594

  
590 595
    case 'equal':
591 596
      $first_entry_key = key($items);
592 597
      $first_entry = array_shift($items);
......
596 601
        }
597 602
      }
598 603
      return $errors;
604

  
599 605
    case 'comparison':
600 606
      foreach (array(1, 2) as $count) {
601 607
        $entry[$count]['key'] = key($items);
......
638 644
            $error = TRUE;
639 645
          }
640 646
          break;
647

  
641 648
        case '>=':
642 649
          if (!($entry[1]['value'] >= $entry[2]['value'])) {
643 650
            $error = TRUE;
644 651
          }
645 652
          break;
653

  
646 654
        case '<':
647 655
          if (!($entry[1]['value'] < $entry[2]['value'])) {
648 656
            $error = TRUE;
649 657
          }
650 658
          break;
659

  
651 660
        case '<=':
652 661
          if (!($entry[1]['value'] <= $entry[2]['value'])) {
653 662
            $error = TRUE;
......
659 668
        $errors[$entry[2]['key']] = _webform_validation_i18n_error_message($rule);
660 669
      }
661 670
      return $errors;
671

  
662 672
    case 'unique':
663 673
      foreach ($items as $key => $val) {
664 674
        $items[$key] = _webform_validation_flatten_array($val);
665 675
      }
666
      // now we count how many times each value appears, and find out which values appear more than once
676
      // Now we count how many times each value appears, and find out which values appear more than once.
667 677
      $items_count = array_count_values(array_map('drupal_strtolower', array_map('trim', $items)));
668 678
      $doubles = array_filter($items_count, create_function('$x', 'return $x > 1;'));
669 679
      foreach ($items as $key => $val) {
......
672 682
        }
673 683
      }
674 684
      return $errors;
685

  
675 686
    case 'specific_value':
676 687
      $specific_values = explode(',', $rule['data']);
677 688
      $specific_values = array_map('trim', $specific_values);
......
682 693
        _webform_validation_test($errors, $key, $rule, $test);
683 694
      }
684 695
      return $errors;
696

  
685 697
    case 'default_value':
686 698
      foreach ($items as $key => $val) {
687 699
        $val = _webform_validation_flatten_array($val);
......
689 701
        _webform_validation_test($errors, $key, $rule, $test);
690 702
      }
691 703
      return $errors;
704

  
692 705
    case 'someofseveral':
693 706
      // Determine the number of components completed.
694 707
      foreach ($items as $key => $val) {
......
730 743
        $errors[$keys[0]] = t('You must complete %verb %compare_number of these items:', array('%verb' => $error, '%compare_number' => $compare_number)) . theme('item_list', array('items' => _webform_validation_get_names_of_rule_components($rule)));
731 744
      }
732 745
      return $errors;
746

  
733 747
    case 'select_min':
734 748
      $min_selections = $rule['data'];
735 749
      foreach ($items as $key => $val) {
......
738 752
        }
739 753
      }
740 754
      return $errors;
755

  
741 756
    case 'select_max':
742 757
      $max_selections = $rule['data'];
743 758
      foreach ($items as $key => $val) {
......
746 761
        }
747 762
      }
748 763
      return $errors;
764

  
749 765
    case 'select_exact':
750 766
      $allowed_selections = $rule['data'];
751 767
      foreach ($items as $key => $val) {
......
758 774
        _webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
759 775
      }
760 776
      return $errors;
777

  
761 778
    case 'plain_text':
762 779
      foreach ($items as $key => $val) {
763 780
        $error_strings = array(
......
771 788
        }
772 789
      }
773 790
      return $errors;
791

  
774 792
    case 'starts_with':
775 793
    case 'ends_with':
776 794
      $pattern = preg_quote($rule['data'], '/');
......
796 814
        }
797 815
      }
798 816
      return $errors;
817

  
799 818
    case 'pattern':
800 819
      $pattern = preg_quote($rule['data'], '/');
801 820
      $pattern = str_replace('@', '[a-zA-Z]', $pattern);
......
810 829
        }
811 830
      }
812 831
      return $errors;
832

  
813 833
    case 'regex':
814 834
    case 'regexi':
815 835
      mb_regex_encoding('UTF-8');
......
827 847
        }
828 848
      }
829 849
      return $errors;
850

  
830 851
    case 'must_be_empty':
831 852
      foreach ($items as $key => $val) {
832 853
        if (count($val) !== 0) {
......
834 855
        }
835 856
      }
836 857
      return $errors;
858

  
837 859
    case 'blacklist':
838 860
      $blacklist = preg_quote($rule['data'], '/');
839 861
      $blacklist = explode(',', $blacklist);
......
846 868
        }
847 869
      }
848 870
      return $errors;
871

  
849 872
    case 'username':
850 873
      foreach ($items as $key => $val) {
851 874
        $error_strings = array(
......
859 882
        }
860 883
      }
861 884
      return $errors;
885

  
862 886
    case 'valid_url':
863 887
      foreach ($items as $key => $val) {
864 888
        $error_strings = array(
......
872 896
        }
873 897
      }
874 898
      return $errors;
899

  
875 900
    case 'email_verify':
876 901
      if (module_exists('email_verify')) {
877 902
        foreach ($items as $key => $val) {
......
890 915
/**
891 916
 * Return an array of the names of the components in a validation rule.
892 917
 *
893
 * @param $rule array
918
 * @param array $rule
894 919
 *   Array of information about a validation rule.
895 920
 *
896 921
 * @return array
......
947 972
}
948 973

  
949 974
/**
950
 * Helper function to deal with submitted values that are arrays (for example, multiple select component).
951
 * We flatten the array as a comma-separated list to do the comparison.
975
 * Helper function to deal with submitted values that are arrays.
976
 *
977
 * For example, multiple select component. We flatten the array as a
978
 * comma-separated list to do the comparison.
952 979
 */
953 980
function _webform_validation_flatten_array($val) {
954 981
  if (is_array($val)) {
......
959 986
}
960 987

  
961 988
/**
962
 * Get a list of validator definitions.
989
 * Get a array of validator definitions.
963 990
 *
964
 * @return
965
 *   Array of information about validators.
991
 * @return array
992
 *   Information about validators.
966 993
 */
967 994
function webform_validation_get_validators() {
995
  static $validators;
996
  if ($validators) {
997
    return $validators;
998
  }
999

  
968 1000
  $validators = module_invoke_all('webform_validation_validators');
969 1001
  // Let modules use hook_webform_validator_alter($validators) to change validator settings.
970 1002
  drupal_alter('webform_validator', $validators);
......
997 1029
}
998 1030

  
999 1031
/**
1000
 * Get a list of valid component types per validator, as defined via hook_webform_validation_validators().
1001
 * If 'all' is specified, all available component types will be returned.
1032
 * Get a list of valid component types per validator.
1033
 *
1034
 * These are defined via hook_webform_validation_validators(). If "all" is
1035
 * specified, all available component types will be returned.
1002 1036
 */
1003 1037
function webform_validation_valid_component_types($validator) {
1004 1038
  $validators = webform_validation_get_validators();
......
1014 1048
/**
1015 1049
 * Return information about a specified validator.
1016 1050
 *
1017
 * @param $validator_key string
1051
 * @param string $validator_key
1018 1052
 *   The key of the validator to return information about.
1019 1053
 *
1020 1054
 * @return array
......
1026 1060
}
1027 1061

  
1028 1062
/**
1029
 * Handle translatable error messages, if available
1063
 * Handle translatable error messages, if available.
1030 1064
 */
1031 1065
function _webform_validation_i18n_error_message($rule) {
1032 1066
  $rule['error_message'] = filter_xss($rule['error_message']);
......
1056 1090
}
1057 1091

  
1058 1092
/**
1059
 * Process the numeric value validation range that was provided in the numeric validator options
1093
 * Process the numeric value validation range that was provided in the numeric validator options.
1060 1094
 */
1061 1095
function _webform_numeric_check_data($data) {
1062 1096
  $range = array('min' => NULL, 'max' => NULL);
1063
  // if no value was specified, don't validate
1097
  // If no value was specified, don't validate.
1064 1098
  if ($data == '') {
1065 1099
    return $range;
1066 1100
  }
1067 1101

  
1068
  // If only one numeric value was specified, this is the min value
1102
  // If only one numeric value was specified, this is the min value.
1069 1103
  if (is_numeric($data)) {
1070 1104
    $range['min'] = (float) $data;
1071 1105
  }

Formats disponibles : Unified diff