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
// | Authors: Bertrand Mansion <bmansion@mamasam.com>                     |
16
// | Authors: Bertrand Mansion <bmansion@mamasam.com>                     |
17
// +----------------------------------------------------------------------+
17
// +----------------------------------------------------------------------+
18
//
18
//
19
// $Id: Callback.php,v 1.1 2005-03-30 08:50:33 jpm Exp $
19
// $Id: Callback.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
* Validates values using callback functions or methods
24
* Validates values using callback functions or methods
25
* @version     1.0
25
* @version     1.0
26
*/
26
*/
27
class HTML_QuickForm_Rule_Callback extends HTML_QuickForm_Rule
27
class HTML_QuickForm_Rule_Callback extends HTML_QuickForm_Rule
28
{
28
{
29
    /**
29
    /**
30
     * Array of callbacks
30
     * Array of callbacks
31
     *
31
     *
32
     * Array is in the format:
32
     * Array is in the format:
33
     * $_data['rulename'] = array('functionname', 'classname');
33
     * $_data['rulename'] = array('functionname', 'classname');
34
     * If the callback is not a method, then the class name is not set.
34
     * If the callback is not a method, then the class name is not set.
35
     *
35
     *
36
     * @var     array
36
     * @var     array
37
     * @access  private
37
     * @access  private
38
     */
38
     */
39
    var $_data = array();
39
    var $_data = array();
40
 
40
 
41
   /**
41
   /**
42
    * Whether to use BC mode for specific rules
42
    * Whether to use BC mode for specific rules
43
    * 
43
    * 
44
    * Previous versions of QF passed element's name as a first parameter
44
    * Previous versions of QF passed element's name as a first parameter
45
    * to validation functions, but not to validation methods. This behaviour
45
    * to validation functions, but not to validation methods. This behaviour
46
    * is emulated if you are using 'function' as rule type when registering.
46
    * is emulated if you are using 'function' as rule type when registering.
47
    * 
47
    * 
48
    * @var array
48
    * @var array
49
    * @access private
49
    * @access private
50
    */
50
    */
51
    var $_BCMode = array();
51
    var $_BCMode = array();
52
 
52
 
53
    /**
53
    /**
54
     * Validates a value using a callback
54
     * Validates a value using a callback
55
     *
55
     *
56
     * @param     string    $value      Value to be checked
56
     * @param     string    $value      Value to be checked
57
     * @param     mixed     $options    Options for callback
57
     * @param     mixed     $options    Options for callback
58
     * @access    public
58
     * @access    public
59
     * @return    boolean   true if value is valid
59
     * @return    boolean   true if value is valid
60
     */
60
     */
61
    function validate($value, $options = null)
61
    function validate($value, $options = null)
62
    {
62
    {
63
        if (isset($this->_data[$this->name])) {
63
        if (isset($this->_data[$this->name])) {
64
            $callback = $this->_data[$this->name];
64
            $callback = $this->_data[$this->name];
65
            if (isset($callback[1])) {
65
            if (isset($callback[1])) {
66
                return call_user_func(array($callback[1], $callback[0]), $value, $options);
66
                return call_user_func(array($callback[1], $callback[0]), $value, $options);
67
            } elseif ($this->_BCMode[$this->name]) {
67
            } elseif ($this->_BCMode[$this->name]) {
68
                return $callback[0]('', $value, $options);
68
                return $callback[0]('', $value, $options);
69
            } else {
69
            } else {
70
                return $callback[0]($value, $options);
70
                return $callback[0]($value, $options);
71
            }
71
            }
72
        } elseif (is_callable($options)) {
72
        } elseif (is_callable($options)) {
73
            return call_user_func($options, $value);
73
            return call_user_func($options, $value);
74
        } else {
74
        } else {
75
            return true;
75
            return true;
76
        }
76
        }
77
    } // end func validate
77
    } // end func validate
78
 
78
 
79
    /**
79
    /**
80
     * Adds new callbacks to the callbacks list
80
     * Adds new callbacks to the callbacks list
81
     *
81
     *
82
     * @param     string    $name       Name of rule
82
     * @param     string    $name       Name of rule
83
     * @param     string    $callback   Name of function or method
83
     * @param     string    $callback   Name of function or method
84
     * @param     string    $class      Name of class containing the method
84
     * @param     string    $class      Name of class containing the method
85
     * @param     bool      $BCMode     Backwards compatibility mode 
85
     * @param     bool      $BCMode     Backwards compatibility mode 
86
     * @access    public
86
     * @access    public
87
     */
87
     */
88
    function addData($name, $callback, $class = null, $BCMode = false)
88
    function addData($name, $callback, $class = null, $BCMode = false)
89
    {
89
    {
90
        if (!empty($class)) {
90
        if (!empty($class)) {
91
            $this->_data[$name] = array($callback, $class);
91
            $this->_data[$name] = array($callback, $class);
92
        } else {
92
        } else {
93
            $this->_data[$name] = array($callback);
93
            $this->_data[$name] = array($callback);
94
        }
94
        }
95
        $this->_BCMode[$name] = $BCMode;
95
        $this->_BCMode[$name] = $BCMode;
96
    } // end func addData
96
    } // end func addData
97
 
97
 
98
 
98
 
99
    function getValidationScript($options = null)
99
    function getValidationScript($options = null)
100
    {
100
    {
101
        if (isset($this->_data[$this->name])) {
101
        if (isset($this->_data[$this->name])) {
102
            $callback = $this->_data[$this->name][0];
102
            $callback = $this->_data[$this->name][0];
103
            $params   = ($this->_BCMode[$this->name]? "'', {jsVar}": '{jsVar}') .
103
            $params   = ($this->_BCMode[$this->name]? "'', {jsVar}": '{jsVar}') .
104
                        (isset($options)? ", '{$options}'": '');
104
                        (isset($options)? ", '{$options}'": '');
105
        } else {
105
        } else {
106
            $callback = is_array($options)? $options[1]: $options;
106
            $callback = is_array($options)? $options[1]: $options;
107
            $params   = '{jsVar}';
107
            $params   = '{jsVar}';
108
        }
108
        }
109
        return array('', "{jsVar} != '' && !{$callback}({$params})");
109
        return array('', "{jsVar} != '' && !{$callback}({$params})");
110
    } // end func getValidationScript
110
    } // end func getValidationScript
111
 
111
 
112
} // end class HTML_QuickForm_Rule_Callback
112
} // end class HTML_QuickForm_Rule_Callback
113
?>
113
?>