Range.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Checks that the length of value is within range
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.01 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category HTML
  15. * @package HTML_QuickForm
  16. * @author Bertrand Mansion <bmansion@mamasam.com>
  17. * @copyright 2001-2009 The PHP Group
  18. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  19. * @version CVS: $Id: Range.php,v 1.8 2009/04/04 21:34:04 avb Exp $
  20. * @link http://pear.php.net/package/HTML_QuickForm
  21. */
  22. /**
  23. * Checks that the length of value is within range
  24. *
  25. * @category HTML
  26. * @package HTML_QuickForm
  27. * @author Bertrand Mansion <bmansion@mamasam.com>
  28. * @version Release: 3.2.11
  29. * @since 3.2
  30. */
  31. class HTML_QuickForm_Rule_Range extends HTML_QuickForm_Rule
  32. {
  33. /**
  34. * Validates a value using a range comparison
  35. *
  36. * @param string $value Value to be checked
  37. * @param mixed $options Int for length, array for range
  38. * @access public
  39. * @return boolean true if value is valid
  40. */
  41. public function validate($value, $options)
  42. {
  43. $length = api_strlen($value);
  44. switch ($this->name) {
  45. case 'min_numeric_length':
  46. return (float) $value >= $options;
  47. case 'max_numeric_length':
  48. return (float) $value <= $options;
  49. case 'minlength':
  50. return $length >= $options;
  51. case 'maxlength':
  52. return $length <= $options;
  53. default:
  54. return $length >= $options[0] && $length <= $options[1];
  55. }
  56. }
  57. public function getValidationScript($options = null)
  58. {
  59. switch ($this->name) {
  60. case 'minlength':
  61. $test = '{jsVar}.length < '.$options;
  62. break;
  63. case 'maxlength':
  64. $test = '{jsVar}.length > '.$options;
  65. break;
  66. default:
  67. $test = '({jsVar}.length < '.$options[0].' || {jsVar}.length > '.$options[1].')';
  68. }
  69. return array('', "{jsVar} != '' && {$test}");
  70. }
  71. }