Compare.php 3.0 KB

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