2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Validates values using callback functions or methods
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 Bertrand Mansion <bmansion@mamasam.com>
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 * Validates values using callback functions or methods
33 * @package HTML_QuickForm
34 * @author Bertrand Mansion <bmansion@mamasam.com>
35 * @version Release: 3.2.16
38 class HTML_QuickForm_Rule_Callback extends HTML_QuickForm_Rule
43 * Array is in the format:
44 * $_data['rulename'] = array('functionname', 'classname');
45 * If the callback is not a method, then the class name is not set.
53 * Whether to use BC mode for specific rules
55 * Previous versions of QF passed element's name as a first parameter
56 * to validation functions, but not to validation methods. This behaviour
57 * is emulated if you are using 'function' as rule type when registering.
62 var $_BCMode = array();
65 * Validates a value using a callback
67 * @param string $value Value to be checked
68 * @param mixed $options Options for callback
70 * @return boolean true if value is valid
72 function validate($value, $options = null)
74 if (isset($this->_data[$this->name])) {
75 $callback = $this->_data[$this->name];
76 if (isset($callback[1])) {
77 return call_user_func(array($callback[1], $callback[0]), $value, $options);
78 } elseif ($this->_BCMode[$this->name]) {
79 return $callback[0]('', $value, $options);
81 return $callback[0]($value, $options);
83 } elseif (is_callable($options)) {
84 return call_user_func($options, $value);
88 } // end func validate
91 * Adds new callbacks to the callbacks list
93 * @param string $name Name of rule
94 * @param string $callback Name of function or method
95 * @param string $class Name of class containing the method
96 * @param bool $BCMode Backwards compatibility mode
99 function addData($name, $callback, $class = null, $BCMode = false)
101 if (!empty($class)) {
102 $this->_data[$name] = array($callback, $class);
104 $this->_data[$name] = array($callback);
106 $this->_BCMode[$name] = $BCMode;
107 } // end func addData
110 function getValidationScript($options = null)
112 if (isset($this->_data[$this->name])) {
113 $callback = $this->_data[$this->name][0];
114 $params = ($this->_BCMode[$this->name]? "'', {jsVar}": '{jsVar}') .
115 (isset($options)? ", '{$options}'": '');
117 $callback = is_array($options)? $options[1]: $options;
120 return array('', "{jsVar} != '' && !{$callback}({$params})");
121 } // end func getValidationScript
123 } // end class HTML_QuickForm_Rule_Callback