Compare.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Rule to compare two form fields
  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 Alexey Borzov <avb@php.net>
  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: Compare.php,v 1.7 2009/04/04 21:34:04 avb Exp $
  20. * @link http://pear.php.net/package/HTML_QuickForm
  21. */
  22. /**
  23. * Rule to compare two form fields
  24. *
  25. * The most common usage for this is to ensure that the password
  26. * confirmation field matches the password field
  27. *
  28. * @category HTML
  29. * @package HTML_QuickForm
  30. * @author Alexey Borzov <avb@php.net>
  31. * @version Release: 3.2.11
  32. * @since 3.2
  33. */
  34. class HTML_QuickForm_Rule_Compare extends HTML_QuickForm_Rule
  35. {
  36. /**
  37. * Possible operators to use
  38. * @var array
  39. * @access private
  40. */
  41. var $_operators = array(
  42. 'eq' => '===',
  43. 'neq' => '!==',
  44. 'gt' => '>',
  45. 'gte' => '>=',
  46. 'lt' => '<',
  47. 'lte' => '<=',
  48. '==' => '===',
  49. '!=' => '!=='
  50. );
  51. /**
  52. * Returns the operator to use for comparing the values
  53. *
  54. * @access private
  55. * @param string operator name
  56. * @return string operator to use for validation
  57. */
  58. function _findOperator($name)
  59. {
  60. $name = trim($name);
  61. if (empty($name)) {
  62. return '===';
  63. } elseif (isset($this->_operators[$name])) {
  64. return $this->_operators[$name];
  65. } elseif (in_array($name, $this->_operators)) {
  66. return $name;
  67. } else {
  68. return '===';
  69. }
  70. }
  71. function validate($values, $operator = null)
  72. {
  73. $operator = $this->_findOperator($operator);
  74. if ('===' != $operator && '!==' != $operator) {
  75. $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
  76. } else {
  77. $compareFn = create_function('$a, $b', 'return strval($a) ' . $operator . ' strval($b);');
  78. }
  79. return $compareFn($values[0], $values[1]);
  80. }
  81. function getValidationScript($operator = null)
  82. {
  83. $operator = $this->_findOperator($operator);
  84. if ('===' != $operator && '!==' != $operator) {
  85. $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))";
  86. } else {
  87. $check = "!(String({jsVar}[0]) {$operator} String({jsVar}[1]))";
  88. }
  89. return array('', "'' != {jsVar}[0] && {$check}");
  90. }
  91. }