Subversion Repositories Applications.papyrus

Rev

Rev 320 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 320 Rev 443
1
<?php
1
<?php
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
2
/* vim: set expandtab tabstop=4 shiftwidth=4: */
3
// +----------------------------------------------------------------------+
3
// +----------------------------------------------------------------------+
4
// | PHP version 4.0                                                      |
4
// | PHP version 4.0                                                      |
5
// +----------------------------------------------------------------------+
5
// +----------------------------------------------------------------------+
6
// | Copyright (c) 1997-2003 The PHP Group                                |
6
// | Copyright (c) 1997-2003 The PHP Group                                |
7
// +----------------------------------------------------------------------+
7
// +----------------------------------------------------------------------+
8
// | This source file is subject to version 2.0 of the PHP license,       |
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        |
9
// | that is bundled with this package in the file LICENSE, and is        |
10
// | available at through the world-wide-web at                           |
10
// | available at through the world-wide-web at                           |
11
// | http://www.php.net/license/2_02.txt.                                 |
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   |
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          |
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.               |
14
// | license@php.net so we can mail you a copy immediately.               |
15
// +----------------------------------------------------------------------+
15
// +----------------------------------------------------------------------+
16
// | Author: Alexey Borzov <avb@php.net>                                  |
16
// | Author: Alexey Borzov <avb@php.net>                                  |
17
// +----------------------------------------------------------------------+
17
// +----------------------------------------------------------------------+
18
//
18
//
19
// $Id: Compare.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
19
// $Id: Compare.php,v 1.2 2005-09-20 17:01:22 ddelon Exp $
20
 
20
 
21
require_once 'HTML/QuickForm/Rule.php';
21
require_once 'HTML/QuickForm/Rule.php';
22
 
22
 
23
/**
23
/**
24
 * Rule to compare two form fields
24
 * Rule to compare two form fields
25
 * 
25
 * 
26
 * The most common usage for this is to ensure that the password 
26
 * The most common usage for this is to ensure that the password 
27
 * confirmation field matches the password field
27
 * confirmation field matches the password field
28
 * 
28
 * 
29
 * @access public
29
 * @access public
30
 * @package HTML_QuickForm
30
 * @package HTML_QuickForm
31
 * @version $Revision: 1.1 $
31
 * @version $Revision: 1.2 $
32
 */
32
 */
33
class HTML_QuickForm_Rule_Compare extends HTML_QuickForm_Rule
33
class HTML_QuickForm_Rule_Compare extends HTML_QuickForm_Rule
34
{
34
{
35
   /**
35
   /**
36
    * Possible operators to use
36
    * Possible operators to use
37
    * @var array
37
    * @var array
38
    * @access private
38
    * @access private
39
    */
39
    */
40
    var $_operators = array(
40
    var $_operators = array(
41
        'eq'  => '==',
41
        'eq'  => '==',
42
        'neq' => '!=',
42
        'neq' => '!=',
43
        'gt'  => '>',
43
        'gt'  => '>',
44
        'gte' => '>=',
44
        'gte' => '>=',
45
        'lt'  => '<',
45
        'lt'  => '<',
46
        'lte' => '<='
46
        'lte' => '<='
47
    );
47
    );
48
 
48
 
49
 
49
 
50
   /**
50
   /**
51
    * Returns the operator to use for comparing the values
51
    * Returns the operator to use for comparing the values
52
    * 
52
    * 
53
    * @access private
53
    * @access private
54
    * @param  string     operator name
54
    * @param  string     operator name
55
    * @return string     operator to use for validation
55
    * @return string     operator to use for validation
56
    */
56
    */
57
    function _findOperator($name)
57
    function _findOperator($name)
58
    {
58
    {
59
        if (empty($name)) {
59
        if (empty($name)) {
60
            return '==';
60
            return '==';
61
        } elseif (isset($this->_operators[$name])) {
61
        } elseif (isset($this->_operators[$name])) {
62
            return $this->_operators[$name];
62
            return $this->_operators[$name];
63
        } elseif (in_array($name, $this->_operators)) {
63
        } elseif (in_array($name, $this->_operators)) {
64
            return $name;
64
            return $name;
65
        } else {
65
        } else {
66
            return '==';
66
            return '==';
67
        }
67
        }
68
    }
68
    }
69
 
69
 
70
 
70
 
71
    function validate($values, $operator = null)
71
    function validate($values, $operator = null)
72
    {
72
    {
73
        $operator = $this->_findOperator($operator);
73
        $operator = $this->_findOperator($operator);
74
        if ('==' != $operator && '!=' != $operator) {
74
        if ('==' != $operator && '!=' != $operator) {
75
            $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
75
            $compareFn = create_function('$a, $b', 'return floatval($a) ' . $operator . ' floatval($b);');
76
        } else {
76
        } else {
77
            $compareFn = create_function('$a, $b', 'return $a ' . $operator . ' $b;');
77
            $compareFn = create_function('$a, $b', 'return $a ' . $operator . ' $b;');
78
        }
78
        }
79
        
79
        
80
        return $compareFn($values[0], $values[1]);
80
        return $compareFn($values[0], $values[1]);
81
    }
81
    }
82
 
82
 
83
 
83
 
84
    function getValidationScript($operator = null)
84
    function getValidationScript($operator = null)
85
    {
85
    {
86
        $operator = $this->_findOperator($operator);
86
        $operator = $this->_findOperator($operator);
87
        if ('==' != $operator && '!=' != $operator) {
87
        if ('==' != $operator && '!=' != $operator) {
88
            $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))";
88
            $check = "!(Number({jsVar}[0]) {$operator} Number({jsVar}[1]))";
89
        } else {
89
        } else {
90
            $check = "!({jsVar}[0] {$operator} {jsVar}[1])";
90
            $check = "!({jsVar}[0] {$operator} {jsVar}[1])";
91
        }
91
        }
92
        return array('', "'' != {jsVar}[0] && {$check}");
92
        return array('', "'' != {jsVar}[0] && {$check}");
93
    }
93
    }
94
}
94
}
95
?>
95
?>