Compare.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. 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. function validate($values, $operator = null)
  76. {
  77. $operator = $this->_findOperator($operator);
  78. if ('===' != $operator && '!==' != $operator) {
  79. $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
  80. } else {
  81. $compareFn = create_function('$a, $b', 'return strval($a) ' . $operator . ' strval($b);');
  82. }
  83. return $compareFn($values[0], $values[1]);
  84. }
  85. function getValidationScript($operator = null)
  86. {
  87. $operator = $this->_findOperator($operator);
  88. if ('===' != $operator && '!==' != $operator) {
  89. $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))";
  90. } else {
  91. $check = "!(String({jsVar}[0]) {$operator} String({jsVar}[1]))";
  92. }
  93. return array('', "'' != {jsVar}[0] && {$check}");
  94. }
  95. }