Subversion Repositories Applications.papyrus

Rev

Rev 320 | Details | Compare with Previous | Last modification | View Log | RSS feed

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