Projet

Général

Profil

Révision a2baadd1

Ajouté par Assos Assos il y a environ 10 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/webform_validation/webform_validation.validators.inc
31 31
      'custom_data' => array(
32 32
        'label' => t('Specify numeric validation range'),
33 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')))),
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')))),
35 35
        'required' => FALSE,
36 36
      ),
37 37
      'description' => t('Verifies that user-entered values are numeric, with the option to specify min and / or max values.'),
......
49 49
        'label' => t('Minimum number of characters'),
50 50
        'description' => t('Specify the minimum number of characters that have to be entered to pass validation.'),
51 51
      ),
52
      'description' => t('Verifies that a user-entered value contains at least the specified number of characters'),
52
      'description' => t('Verifies that a user-entered value contains at least the specified number of characters.'),
53 53
    ),
54 54
    'max_length' => array(
55 55
      'name' => t('Maximum length'),
......
64 64
        'label' => t('Maximum number of characters'),
65 65
        'description' => t('Specify the maximum number of characters that can be entered to pass validation.'),
66 66
      ),
67
      'description' => t('Verifies that a user-entered value contains at most the specified number of characters'),
67
      'description' => t('Verifies that a user-entered value contains at most the specified number of characters.'),
68 68
    ),
69 69
    'min_words' => array(
70 70
      'name' => t('Minimum number of words'),
......
171 171
      'custom_error' => TRUE,
172 172
      'description' => t('Verifies that the user-entered value is the default value for that component. Negate if you want not the default value.'),
173 173
    ),
174
    'oneoftwo' => array(
175
      'name' => t('Require at least one of two fields'),
174
    'someofseveral' => array(
175
      'name' => t('At least one or more out of two or more'),
176 176
      'component_types' => array(
177 177
        'number',
178 178
        'textfield',
......
180 180
        'email',
181 181
        'select',
182 182
      ),
183
      'min_components' => 2,
184
      'max_components' => 2,
185
      'description' => t('Forces the user to specify / select at least one of two selected webform components'),
186
    ),
187
    'oneofseveral' => array(
188
      'name' => t('Require at least one of several fields'),
189
      'component_types' => array(
190
        'number',
191
        'textfield',
192
        'textarea',
193
        'email',
194
        'select',
183
      'custom_data' => array(
184
        'label' => t('Minimum number to be completed'),
185
        'description' => t('Specify the minimum number that must be completed. For example, enter 1 if the user must complete at least 1 of the selected components.'),
195 186
      ),
196 187
      'min_components' => 2,
197
      'description' => t('Forces the user to specify / select at least one of several selected webform components.'),
188
      'description' => t('Requires the user to complete at least one or more of two or more Webform components, such as 3 out of 5 or 1 out of 3.'),
198 189
    ),
199 190
    'select_min' => array(
200 191
      'name' => t('Minimum number of selections required'),
......
283 274
      'custom_data' => array(
284 275
        'label' => t('Pattern'),
285 276
        'description' => t('Specify a pattern where:')
286
        . 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'))))
287
        . t('Examples')
288
        . theme('item_list', array('items' => array(t('North American phone number: (###) ###-####'), t('D-2500 series model numbers: D-25##'), t('UK Postal Code: @# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA')))),
277
          . 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'))))
278
          . t('Examples')
279
          . theme('item_list', array('items' => array(t('North American phone number: (###) ###-####'), t('D-2500 series model numbers: D-25##'), t('UK Postal Code: @# #@@|@## #@@|@@# #@@|@@## #@@|@#@ #@@|@@#@ #@@|GIR0AA')))),
289 280
      ),
290 281
      'description' => t('Verifies that a user-entered value follows to a specified pattern.'),
291 282
    ),
......
386 377
 */
387 378
function webform_validation_webform_validation_validate($validator_name, $items, $components, $rule) {
388 379
  if ($items) {
380
    // Some components, such as multiple select, send their values as arrays, others don't.
381
    // Convert all to arrays and write the rules to handle them that way. Remove empty values.
382
    foreach ($items as $key => $val) {
383
      $new_values = array();
384
      foreach ((array) $val as $val_key => $val_val) {
385
        if ((string) $val_val !== '') {
386
          $new_values[$val_key] = $val_val;
387
        }
388
      }
389
      $items[$key] = $new_values;
390
    }
391

  
389 392
    $errors = array();
390 393
    switch ($validator_name) {
391 394
      case 'numeric':
392 395
        $num_range = _webform_numeric_check_data($rule['data']);
393 396
        foreach ($items as $key => $val) {
394
          if ($val != '') {
397
          foreach ($val as $subval) {
395 398
            // first check if the value is numeric
396
            if (is_numeric($val)) {
397
              $val = (float) $val;
399
            if (is_numeric($subval)) {
400
              $subval = (float) $subval;
398 401
            }
399 402
            else {
400 403
              $errors[$key] = t('%item is not numeric.', array('%item' => $components[$key]['name']));
......
405 408
            // a. validate min & max
406 409
            if (isset($num_range['min']) && isset($num_range['max'])) {
407 410
              // validate the min - max range
408
              if ($val < $num_range['min'] || $val > $num_range['max']) {
411
              if ($subval < $num_range['min'] || $subval > $num_range['max']) {
409 412
                $errors[$key] = t('%item is not within the allowed range %range.', array('%item' => $components[$key]['name'], '%range' => str_replace('|' , ' - ', $rule['data'])));
410 413
              }
411 414
            }
412 415
            else {
413 416
              // b. validate min
414 417
              if (isset($num_range['min'])) {
415
                if ($val < $num_range['min']) {
418
                if ($subval < $num_range['min']) {
416 419
                  $errors[$key] = t('%item should be greater than or equal to %val.', array('%item' => $components[$key]['name'], '%val' => $num_range['min']));
417 420
                }
418 421
              }
419 422
              // c. validate max
420 423
              if (isset($num_range['max'])) {
421
                if ($val > $num_range['max']) {
424
                if ($subval > $num_range['max']) {
422 425
                  $errors[$key] = t('%item should be less than or equal to %val.', array('%item' => $components[$key]['name'], '%val' => $num_range['max']));
423 426
                }
424 427
              }
......
429 432
      case 'min_length':
430 433
        $min_length = $rule['data'];
431 434
        foreach ($items as $key => $val) {
432
          if ($val != '' && (drupal_strlen($val) < $min_length)) {
433
            $errors[$key] = t('%item should be at least %num characters long.', array('%item' => $components[$key]['name'], '%num' => $min_length));
435
          foreach ($val as $subval) {
436
            if (drupal_strlen($subval) < $min_length) {
437
              $errors[$key] = t('%item should be at least %num characters long.', array('%item' => $components[$key]['name'], '%num' => $min_length));
438
            }
434 439
          }
435 440
        }
436 441
        return $errors;
437 442
      case 'max_length':
438 443
        $max_length = $rule['data'];
439 444
        foreach ($items as $key => $val) {
440
          if ($val != '' && (drupal_strlen($val) > $max_length)) {
441
            $errors[$key] = t('%item should be at most %num characters long.', array('%item' => $components[$key]['name'], '%num' => $max_length));
445
          foreach ($val as $subval) {
446
            if (drupal_strlen($subval) > $max_length) {
447
              $errors[$key] = t('%item should be at most %num characters long.', array('%item' => $components[$key]['name'], '%num' => $max_length));
448
            }
442 449
          }
443 450
        }
444 451
        return $errors;
445 452
      case 'min_words':
446 453
        $min_words = $rule['data'];
447 454
        foreach ($items as $key => $val) {
448
          if ($val != '' && (_webform_validation_count_words($val) < $min_words)) {
449
            $error = format_plural($min_words, '%item should be at least 1 word long.', '%item should be at least @count words long.', array('%item' => $components[$key]['name']));
450
            $errors[$key] = $error;
455
          foreach ($val as $subval) {
456
            if (_webform_validation_count_words($subval) < $min_words) {
457
              $error = format_plural($min_words, '%item should be at least 1 word long.', '%item should be at least @count words long.', array('%item' => $components[$key]['name']));
458
              $errors[$key] = $error;
459
            }
451 460
          }
452 461
        }
453 462
        return $errors;
454 463
      case 'max_words':
455 464
        $max_words = $rule['data'];
456 465
        foreach ($items as $key => $val) {
457
          if ($val != '' && (_webform_validation_count_words($val) > $max_words)) {
458
            $error = format_plural($max_words, '%item should be at most 1 word long.', '%item should be at most @count words long.', array('%item' => $components[$key]['name']));
459
            $errors[$key] = $error;
466
          foreach ($val as $subval) {
467
            if (_webform_validation_count_words($subval) > $max_words) {
468
              $error = format_plural($max_words, '%item should be at most 1 word long.', '%item should be at most @count words long.', array('%item' => $components[$key]['name']));
469
              $errors[$key] = $error;
470
            }
460 471
          }
461 472
        }
462 473
        return $errors;
463 474
      case 'equal':
464 475
        $first_entry_key = key($items);
465 476
        $first_entry = array_shift($items);
466
        $first_entry = _webform_validation_flatten_array($first_entry); // flatten in case of array
467
        // now check if following components equal the first one
468 477
        foreach ($items as $key => $val) {
469
          $val = _webform_validation_flatten_array($val); // flatten in case of array
470 478
          if ($val !== $first_entry) {
471 479
            $errors[$key] = t('%item_checked does not match %item_first.', array('%item_checked' => $components[$key]['name'], '%item_first' => $components[$first_entry_key]['name']));
472 480
          }
......
537 545
        return $errors;
538 546
      case 'unique':
539 547
        foreach ($items as $key => $val) {
540
          if (is_array($val)) {
541
            // make sure to flatten arrays first
542
            $items[$key] = _webform_validation_flatten_array($val);
543
          }
544
          if (empty($items[$key])) {
545
            // items without a value selected shouldn't be validated
546
            unset($items[$key]);
547
          }
548
          $items[$key] = _webform_validation_flatten_array($val);
548 549
        }
549 550
        // now we count how many times each value appears, and find out which values appear more than once
550 551
        $items_count = array_count_values(array_map('drupal_strtolower', array_map('trim', $items)));
......
559 560
        $specific_values = explode(',', $rule['data']);
560 561
        $specific_values = array_map('trim', $specific_values);
561 562
        foreach ($items as $key => $val) {
562
          if (is_array($val)) {
563
            // Selects: Fail if at least one checked and at least one not in the allowed values.
564
            $val = array_filter($val);
565
            $test = count($val) && count(array_diff($val, $specific_values));
566
          }
567
          else {
568
            // Fail if not empty and not in the allowed values.
569
            $test = $val && !in_array($val, $specific_values);
570
          }
563
          // Selects: Fail if at least one checked and at least one not in the allowed values.
564
          $val = array_filter($val);
565
          $test = count($val) && count(array_diff($val, $specific_values));
571 566
          _webform_validation_test($errors, $key, $rule, $test);
572 567
        }
573 568
        return $errors;
574 569
      case 'default_value':
575 570
        foreach ($items as $key => $val) {
576
          if (is_array($val)) {
577
            $val = _webform_validation_flatten_array($val);
578
          }
571
          $val = _webform_validation_flatten_array($val);
579 572
          $test = $val != $components[$key]['value'];
580 573
          _webform_validation_test($errors, $key, $rule, $test);
581 574
        }
582 575
        return $errors;
583
      case 'oneoftwo':
584
        // $components should have 2 items
585
        $keys = array_keys($items);
586
        $item1 = array_shift($keys);
587
        $item2 = array_shift($keys);
588
        $entry1 = _webform_validation_flatten_array($items[$item1]);
589
        $entry2 = _webform_validation_flatten_array($items[$item2]);
590
        if (empty($entry1) && empty($entry2)) {
591
          return array($item1 => t('You have to specify %item1 or %item2 (or both).', array('%item1' => $components[$item1]['name'], '%item2' => $components[$item2]['name'])));
592
        }
593
        return;
594
      case 'oneofseveral':
576
      case 'someofseveral':
595 577
        foreach ($items as $key => $val) {
596
          if (is_array($val)) {
597
            // make sure to flatten arrays first
598
            $items[$key] = _webform_validation_flatten_array($val);
599
          }
578
          $items[$key] = _webform_validation_flatten_array($val);
579
          $items[$key] = (bool) strlen($items[$key]);
600 580
        }
601
        // $components should have at least one of several items
602
        if (count(array_filter($items)) < 1) {
581
        $min_selections = isset($rule['data']) ? ((int) $rule['data']) : 1;
582
        if (count(array_filter($items)) < $min_selections) {
603 583
          $keys = array_keys($items);
604 584
          $names = array();
605 585
          foreach ($keys as $value) {
606 586
            $names[] = _webform_filter_xss($components[$value]['name']);
607 587
          }
608
          return array($keys[0] => t('You have to specify at least one of these items:') . theme('item_list', array('items' => $names)));
588
          return array($keys[0] => t('You must complete at least %min_selections of these items:', array('%min_selections' => $min_selections)) . theme('item_list', array('items' => $names)));
609 589
        }
610 590
        return;
611 591
      case 'select_min':
612 592
        $min_selections = $rule['data'];
613 593
        foreach ($items as $key => $val) {
614
          if (is_array($val) && (count(array_filter($val, '_webform_validation_check_false')) < $min_selections)) {
594
          if (count(array_filter($val, '_webform_validation_check_false')) < $min_selections) {
615 595
            $errors[$key] = t('Please select at least %num options for %item.', array('%num' => $min_selections, '%item' => $components[$key]['name']));
616 596
          }
617 597
        }
......
619 599
      case 'select_max':
620 600
        $max_selections = $rule['data'];
621 601
        foreach ($items as $key => $val) {
622
          if (is_array($val) && (count(array_filter($val, '_webform_validation_check_false')) > $max_selections)) {
602
          if (count(array_filter($val, '_webform_validation_check_false')) > $max_selections) {
623 603
            $errors[$key] = t('Please select maximum %num options for %item.', array('%num' => $max_selections, '%item' => $components[$key]['name']));
624 604
          }
625 605
        }
......
632 612
            'negated' => 'Please do not select %num options for %item.',
633 613
          );
634 614
          $error_vars = array('%num' => $allowed_selections, '%item' => $components[$key]['name']);
635
          $test = is_array($val) && (count(array_filter($val, '_webform_validation_check_false')) != $allowed_selections);
615
          $test = count(array_filter($val, '_webform_validation_check_false')) != $allowed_selections;
636 616
          _webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
637 617
        }
638 618
        return $errors;
......
643 623
            'negated' => '%item must contain HTML tags.',
644 624
          );
645 625
          $error_vars = array('%item' => $components[$key]['name']);
646
          $test = $val != '' && (strcmp($val, strip_tags($val)));
647
          _webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
626
          foreach ($val as $subval) {
627
            $test = strcmp($subval, strip_tags($subval));
628
            _webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
629
          }
648 630
        }
649 631
        return $errors;
650 632
      case 'starts_with':
......
661 643
        $pattern = '/' . $pattern . '/';
662 644

  
663 645
        foreach ($items as $key => $val) {
664
          $val = (string) $val;
665 646
          $error_strings = array(
666 647
            'regular' => '%item must %verb with "%string".',
667 648
            'negated' => '%item must not %verb with "%string".',
668 649
          );
669 650
          $error_vars = array('%item' => $components[$key]['name'], '%verb' => $verb, '%string' => $rule['data']);
670
          $test = $val && !preg_match($pattern, $val);
671
          _webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
651
          foreach ($val as $subval) {
652
            $test = !preg_match($pattern, $subval);
653
            _webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
654
          }
672 655
        }
673 656
        return $errors;
674 657
      case 'pattern':
......
679 662
        // Un-escape "|" operator.
680 663
        $pattern = preg_replace('/(\\s*)(\\\\)(\\|)(\\s*)/', ')|(', $pattern);
681 664
        foreach ($items as $key => $val) {
682
          $val = (string) $val;
683
          $test = $val !== '' && !preg_match('/^(' . $pattern . ')$/', $val);
684
          _webform_validation_test($errors, $key, $rule, $test);
665
          foreach ($val as $subval) {
666
            $test = !preg_match('/^(' . $pattern . ')$/', $subval);
667
            _webform_validation_test($errors, $key, $rule, $test);
668
          }
685 669
        }
686 670
        return $errors;
687 671
      case 'regex':
......
689 673
        mb_regex_encoding('UTF-8');
690 674
        $regex = $rule['data'];
691 675
        foreach ($items as $key => $val) {
692
          $val = (string) $val;
693
          if ($val) {
676
          foreach ($val as $subval) {
694 677
            if ($validator_name === 'regexi') {
695
              $match = (bool) mb_eregi($regex, $val);
678
              $match = (bool) mb_eregi($regex, $subval);
696 679
            }
697 680
            else {
698
              $match = (bool) mb_ereg($regex, $val);
681
              $match = (bool) mb_ereg($regex, $subval);
699 682
            }
700 683
            $test = !$match;
701 684
            _webform_validation_test($errors, $key, $rule, $test);
......
704 687
        return $errors;
705 688
      case 'must_be_empty':
706 689
        foreach ($items as $key => $val) {
707
          if ($val) {
690
          if (count($val) !== 0) {
708 691
            $errors[$key] = t('%item does not contain the correct data.', array('%item' => $components[$key]['name']));
709 692
          }
710 693
        }
......
715 698
        $blacklist = array_map('trim', $blacklist);
716 699
        $blacklist = '/\b(' . implode('|', $blacklist) . ')\b/i';
717 700
        foreach ($items as $key => $val) {
718
          $test = $val != '' && preg_match($blacklist, $val);
719
          _webform_validation_test($errors, $key, $rule, $test);
701
          foreach ($val as $subval) {
702
            $test = preg_match($blacklist, $subval);
703
            _webform_validation_test($errors, $key, $rule, $test);
704
          }
720 705
        }
721 706
        return $errors;
722 707
      case 'username':
......
726 711
            'negated' => 'The %item field matches an active username.',
727 712
          );
728 713
          $error_vars = array('%item' => $components[$key]['name']);
729
          $test = $val != '' && !db_query_range('SELECT 1 FROM {users} WHERE name = :username AND status = 1', 0, 1, array(':username' => $val))->fetchField();
730
          _webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
714
          foreach ($val as $subval) {
715
            $test = !db_query_range('SELECT 1 FROM {users} WHERE name = :username AND status = 1', 0, 1, array(':username' => $subval))->fetchField();
716
            _webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
717
          }
731 718
        }
732 719
        return $errors;
733 720
      case 'valid_url':
......
737 724
            'negated' => '%item must not be a valid URL.',
738 725
          );
739 726
          $error_vars = array('%item' => $components[$key]['name']);
740
          $test = $val && !valid_url($val, TRUE);
741
          _webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
727
          foreach ($val as $subval) {
728
            $test = !valid_url($subval, TRUE);
729
            _webform_validation_test($errors, $key, $rule, $test, $error_strings, $error_vars);
730
          }
742 731
        }
743 732
        return $errors;
744 733
      case 'email_verify':
745 734
        if (module_exists('email_verify')) {
746 735
          foreach ($items as $key => $val) {
747
            $error = email_verify_check($val);
748
            if ($error) {
749
              $errors[$key] = $error;
736
            foreach ($val as $subval) {
737
              $error = email_verify_check($subval);
738
              if ($error) {
739
                $errors[$key] = $error;
740
              }
750 741
            }
751 742
          }
752 743
        }
......
776 767

  
777 768
/**
778 769
 * Count the number of words in a value.
770
 *
771
 * Strip HTML first.
779 772
 */
780 773
function _webform_validation_count_words($val) {
781
  // Deal with values from HTML textareas.
782
  if (is_array($val) && isset($val['value'])) {
783
    $val = strip_tags($val['value']);
784
    // Replace entities representing spaces with actual spaces.
785
    $val = str_replace('&nbsp;', ' ', $val);
786
    $val = str_replace('&#160;', ' ', $val);
787
  }
788
  else {
789
    $val = _webform_validation_flatten_array($val);
790
  }
774
  $val = _webform_validation_flatten_array($val);
775

  
776
  $val = strip_tags($val);
777
  // Replace entities representing spaces with actual spaces.
778
  $val = str_replace('&nbsp;', ' ', $val);
779
  $val = str_replace('&#160;', ' ', $val);
780

  
791 781
  return str_word_count($val);
792 782
}
793 783

  
......
804 794
}
805 795

  
806 796
/**
807
 * Helper function to deal with submitted values that are arrays (e.g. multiple select component)
797
 * Helper function to deal with submitted values that are arrays (for example, multiple select component).
808 798
 * We flatten the array as a comma-separated list to do the comparison.
809 799
 */
810 800
function _webform_validation_flatten_array($val) {
......
883 873
 */
884 874
function _webform_validation_i18n_error_message($rule) {
885 875
  $rule['error_message'] = filter_xss($rule['error_message']);
886
  if (module_exists('i18nstrings')) {
887
    return i18nstrings('webform_validation:error_message:' . $rule['ruleid'] . ':message', $rule['error_message']);
876
  if (module_exists('i18n')) {
877
    return i18n_string('webform_validation:error_message:' . $rule['ruleid'] . ':message', $rule['error_message']);
888 878
  }
889 879
  return $rule['error_message'];
890 880
}

Formats disponibles : Unified diff