2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Rule to compare two form fields
9 * LICENSE: This source file is subject to version 3.01 of the PHP license
10 * that is available through the world-wide-web at the following URI:
11 * http://www.php.net/license/3_01.txt If you did not receive a copy of
12 * the PHP License and are unable to obtain it through the web, please
13 * send a note to license@php.net so we can mail you a copy immediately.
16 * @package HTML_QuickForm
17 * @author Alexey Borzov <avb@php.net>
18 * @copyright 2001-2011 The PHP Group
19 * @license http://www.php.net/license/3_01.txt PHP License 3.01
21 * @link http://pear.php.net/package/HTML_QuickForm
25 * Abstract base class for QuickForm validation rules
27 require_once 'HTML/QuickForm/Rule.php';
30 * Rule to compare two form fields
32 * The most common usage for this is to ensure that the password
33 * confirmation field matches the password field
36 * @package HTML_QuickForm
37 * @author Alexey Borzov <avb@php.net>
38 * @version Release: 3.2.16
41 class HTML_QuickForm_Rule_Compare extends HTML_QuickForm_Rule
44 * Possible operators to use
48 var $_operators = array(
61 * Returns the operator to use for comparing the values
64 * @param string operator name
65 * @return string operator to use for validation
67 function _findOperator($name)
71 } elseif (isset($this->_operators[$name])) {
72 return $this->_operators[$name];
73 } elseif (in_array($name, $this->_operators)) {
81 function validate($values, $operator = null)
83 $operator = $this->_findOperator($operator);
84 if ('===' != $operator && '!==' != $operator) {
85 $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
87 $compareFn = create_function('$a, $b', 'return strval($a) ' . $operator . ' strval($b);');
90 return $compareFn($values[0], $values[1]);
94 function getValidationScript($operator = null)
96 $operator = $this->_findOperator($operator);
97 if ('===' != $operator && '!==' != $operator) {
98 $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))";
100 $check = "!(String({jsVar}[0]) {$operator} String({jsVar}[1]))";
102 return array('', "'' != {jsVar}[0] && {$check}");