2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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 // +----------------------------------------------------------------------+
21 require_once 'HTML/QuickForm/Rule.php';
24 * Rule to compare two form fields
26 * The most common usage for this is to ensure that the password
27 * confirmation field matches the password field
30 * @package HTML_QuickForm
33 class HTML_QuickForm_Rule_Compare
extends HTML_QuickForm_Rule
36 * Possible operators to use
40 var $_operators = array(
51 * Returns the operator to use for comparing the values
54 * @param string operator name
55 * @return string operator to use for validation
57 function _findOperator($name)
61 } elseif (isset($this->_operators
[$name])) {
62 return $this->_operators
[$name];
63 } elseif (in_array($name, $this->_operators
)) {
71 function validate($values, $operator = null)
73 $operator = $this->_findOperator($operator);
74 if ('==' != $operator && '!=' != $operator) {
75 $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
77 $compareFn = create_function('$a, $b', 'return $a ' . $operator . ' $b;');
80 return $compareFn($values[0], $values[1]);
84 function getValidationScript($operator = null)
86 $operator = $this->_findOperator($operator);
87 if ('==' != $operator && '!=' != $operator) {
88 $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))";
90 $check = "!({jsVar}[0] {$operator} {jsVar}[1])";
92 return array('', "'' != {jsVar}[0] && {$check}");