adding some strings
[moodle-linuxchix.git] / lib / pear / HTML / QuickForm / Rule / Callback.php
blobcba44f4ce203ddd8db17d4a1b47d09f98d4e0840
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 // +----------------------------------------------------------------------+
19 // $Id$
21 require_once('HTML/QuickForm/Rule.php');
23 /**
24 * Validates values using callback functions or methods
25 * @version 1.0
27 class HTML_QuickForm_Rule_Callback extends HTML_QuickForm_Rule
29 /**
30 * Array of callbacks
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.
36 * @var array
37 * @access private
39 var $_data = array();
41 /**
42 * Whether to use BC mode for specific rules
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.
48 * @var array
49 * @access private
51 var $_BCMode = array();
53 /**
54 * Validates a value using a callback
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
61 function validate($value, $options = null)
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);
72 } elseif (is_callable($options)) {
73 return call_user_func($options, $value);
74 } else {
75 return true;
77 } // end func validate
79 /**
80 * Adds new callbacks to the callbacks list
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
88 function addData($name, $callback, $class = null, $BCMode = false)
90 if (!empty($class)) {
91 $this->_data[$name] = array($callback, $class);
92 } else {
93 $this->_data[$name] = array($callback);
95 $this->_BCMode[$name] = $BCMode;
96 } // end func addData
99 function getValidationScript($options = null)
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}';
109 return array('', "{jsVar} != '' && !{$callback}({$params})");
110 } // end func getValidationScript
112 } // end class HTML_QuickForm_Rule_Callback