Compare.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4: */
  3. // +----------------------------------------------------------------------+
  4. // | PHP version 4.0 |
  5. // +----------------------------------------------------------------------+
  6. // | Copyright (c) 1997-2003 The PHP Group |
  7. // +----------------------------------------------------------------------+
  8. // | This source file is subject to version 2.0 of the PHP license, |
  9. // | that is bundled with this package in the file LICENSE, and is |
  10. // | available at through the world-wide-web at |
  11. // | http://www.php.net/license/2_02.txt. |
  12. // | If you did not receive a copy of the PHP license and are unable to |
  13. // | obtain it through the world-wide-web, please send a note to |
  14. // | license@php.net so we can mail you a copy immediately. |
  15. // +----------------------------------------------------------------------+
  16. // | Author: Alexey Borzov <avb@php.net> |
  17. // +----------------------------------------------------------------------+
  18. //
  19. // $Id: Compare.php 6184 2005-09-07 10:08:17Z bmol $
  20. require_once 'HTML/QuickForm/Rule.php';
  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. * @access public
  28. * @package HTML_QuickForm
  29. * @version $Revision: 6184 $
  30. */
  31. class HTML_QuickForm_Rule_Compare extends HTML_QuickForm_Rule
  32. {
  33. /**
  34. * Possible operators to use
  35. * @var array
  36. * @access private
  37. */
  38. var $_operators = array(
  39. 'eq' => '==',
  40. 'neq' => '!=',
  41. 'gt' => '>',
  42. 'gte' => '>=',
  43. 'lt' => '<',
  44. 'lte' => '<='
  45. );
  46. /**
  47. * Returns the operator to use for comparing the values
  48. *
  49. * @access private
  50. * @param string operator name
  51. * @return string operator to use for validation
  52. */
  53. function _findOperator($name)
  54. {
  55. if (empty($name)) {
  56. return '==';
  57. } elseif (isset($this->_operators[$name])) {
  58. return $this->_operators[$name];
  59. } elseif (in_array($name, $this->_operators)) {
  60. return $name;
  61. } else {
  62. return '==';
  63. }
  64. }
  65. function validate($values, $operator = null)
  66. {
  67. $operator = $this->_findOperator($operator);
  68. if ('==' != $operator && '!=' != $operator) {
  69. $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
  70. } else {
  71. $compareFn = create_function('$a, $b', 'return $a ' . $operator . ' $b;');
  72. }
  73. return $compareFn($values[0], $values[1]);
  74. }
  75. function getValidationScript($operator = null)
  76. {
  77. $operator = $this->_findOperator($operator);
  78. if ('==' != $operator && '!=' != $operator) {
  79. $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))";
  80. } else {
  81. $check = "!({jsVar}[0] {$operator} {jsVar}[1])";
  82. }
  83. return array('', "'' != {jsVar}[0] && {$check}");
  84. }
  85. }
  86. ?>