Compare.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. /**
  3. * Rule to compare two form fields
  4. *
  5. * PHP versions 4 and 5
  6. *
  7. * LICENSE: This source file is subject to version 3.01 of the PHP license
  8. * that is available through the world-wide-web at the following URI:
  9. * http://www.php.net/license/3_01.txt If you did not receive a copy of
  10. * the PHP License and are unable to obtain it through the web, please
  11. * send a note to license@php.net so we can mail you a copy immediately.
  12. *
  13. * @category HTML
  14. * @package HTML_QuickForm
  15. * @author Alexey Borzov <avb@php.net>
  16. * @copyright 2001-2009 The PHP Group
  17. * @license http://www.php.net/license/3_01.txt PHP License 3.01
  18. * @version CVS: $Id: Compare.php,v 1.7 2009/04/04 21:34:04 avb Exp $
  19. * @link http://pear.php.net/package/HTML_QuickForm
  20. */
  21. /**
  22. * Rule to compare two form fields
  23. *
  24. * The most common usage for this is to ensure that the password
  25. * confirmation field matches the password field
  26. *
  27. * @category HTML
  28. * @package HTML_QuickForm
  29. * @author Alexey Borzov <avb@php.net>
  30. * @version Release: 3.2.11
  31. * @since 3.2
  32. */
  33. class HTML_QuickForm_Rule_Compare extends HTML_QuickForm_Rule
  34. {
  35. /**
  36. * Possible operators to use
  37. * @var array
  38. * @access private
  39. */
  40. public $_operators = array(
  41. 'eq' => '===',
  42. 'neq' => '!==',
  43. 'gt' => '>',
  44. 'gte' => '>=',
  45. 'lt' => '<',
  46. 'lte' => '<=',
  47. '==' => '===',
  48. '!=' => '!=='
  49. );
  50. /**
  51. * Returns the operator to use for comparing the values
  52. *
  53. * @access private
  54. * @param string operator name
  55. * @return string operator to use for validation
  56. */
  57. public function _findOperator($name)
  58. {
  59. $name = trim($name);
  60. if (empty($name)) {
  61. return '===';
  62. } elseif (isset($this->_operators[$name])) {
  63. return $this->_operators[$name];
  64. } elseif (in_array($name, $this->_operators)) {
  65. return $name;
  66. } else {
  67. return '===';
  68. }
  69. }
  70. /**
  71. * @param array $values
  72. * @param string $operator
  73. * @return mixed
  74. */
  75. public function validate($values, $operator = null)
  76. {
  77. $operator = $this->_findOperator($operator);
  78. $a = $values[0];
  79. $b = $values[1];
  80. if ('===' != $operator && '!==' != $operator) {
  81. $a = floatval($a);
  82. $b = floatval($b);
  83. } else {
  84. $a = strval($a);
  85. $b = strval($b);
  86. }
  87. switch ($operator) {
  88. case '===':
  89. return $a === $b;
  90. break;
  91. case '!==':
  92. return $a !== $b;
  93. break;
  94. case '>':
  95. return $a > $b;
  96. break;
  97. case '>=':
  98. return $a >= $b;
  99. break;
  100. case '<':
  101. return $a < $b;
  102. break;
  103. case '<=':
  104. return $a <= $b;
  105. break;
  106. }
  107. }
  108. public function getValidationScript($operator = null)
  109. {
  110. $operator = $this->_findOperator($operator);
  111. if ('===' != $operator && '!==' != $operator) {
  112. $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))";
  113. } else {
  114. $check = "!(String({jsVar}[0]) {$operator} String({jsVar}[1]))";
  115. }
  116. return array('', "'' != {jsVar}[0] && {$check}");
  117. }
  118. }