2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Registers rule objects and uses them for validation
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 Adam Daniel <adaniel1@eesus.jnj.com>
18 * @author Bertrand Mansion <bmansion@mamasam.com>
19 * @author Alexey Borzov <avb@php.net>
20 * @copyright 2001-2011 The PHP Group
21 * @license http://www.php.net/license/3_01.txt PHP License 3.01
23 * @link http://pear.php.net/package/HTML_QuickForm
27 * Registers rule objects and uses them for validation
30 * @package HTML_QuickForm
31 * @author Adam Daniel <adaniel1@eesus.jnj.com>
32 * @author Bertrand Mansion <bmansion@mamasam.com>
33 * @author Alexey Borzov <avb@php.net>
34 * @version Release: 3.2.16
37 class HTML_QuickForm_RuleRegistry
40 * Array containing references to used rules
44 var $_rules = array();
48 * Returns a singleton of HTML_QuickForm_RuleRegistry
50 * Usually, only one RuleRegistry object is needed, this is the reason
51 * why it is recommended to use this method to get the validation object.
55 * @return HTML_QuickForm_RuleRegistry
61 $obj = new HTML_QuickForm_RuleRegistry();
64 } // end func singleton
67 * Registers a new validation rule
69 * In order to use a custom rule in your form, you need to register it
70 * first. For regular expressions, one can directly use the 'regex' type
71 * rule in addRule(), this is faster than registering the rule.
73 * Functions and methods can be registered. Use the 'function' type.
74 * When registering a method, specify the class name as second parameter.
76 * You can also register an HTML_QuickForm_Rule subclass with its own
79 * @param string $ruleName Name of validation rule
80 * @param string $type Either: 'regex', 'function' or null
81 * @param string $data1 Name of function, regular expression or
82 * HTML_QuickForm_Rule object class name
83 * @param string $data2 Object parent of above function or HTML_QuickForm_Rule file path
87 function registerRule($ruleName, $type, $data1, $data2 = null)
89 $type = strtolower($type);
90 if ($type == 'regex') {
92 $rule =& $this->getRule('regex');
93 $rule->addData($ruleName, $data1);
94 $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = $GLOBALS['_HTML_QuickForm_registered_rules']['regex'];
96 } elseif ($type == 'function' || $type == 'callback') {
98 $rule =& $this->getRule('callback');
99 $rule->addData($ruleName, $data1, $data2, 'function' == $type);
100 $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = $GLOBALS['_HTML_QuickForm_registered_rules']['callback'];
102 } elseif (is_object($data1)) {
103 // An instance of HTML_QuickForm_Rule
104 $this->_rules[strtolower(get_class($data1))] = $data1;
105 $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = array(strtolower(get_class($data1)), null);
109 $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = array(strtolower($data1), $data2);
111 } // end func registerRule
114 * Returns a reference to the requested rule object
116 * @param string $ruleName Name of the requested rule
118 * @return HTML_QuickForm_Rule
120 function &getRule($ruleName)
122 list($class, $path) = $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName];
124 if (!isset($this->_rules[$class])) {
128 $this->_rules[$class] = new $class();
130 $this->_rules[$class]->setName($ruleName);
131 return $this->_rules[$class];
132 } // end func getRule
135 * Performs validation on the given values
137 * @param string $ruleName Name of the rule to be used
138 * @param mixed $values Can be a scalar or an array of values
140 * @param mixed $options Options used by the rule
141 * @param mixed $multiple Whether to validate an array of values altogether
143 * @return mixed true if no error found, int of valid values (when an array of values is given) or false if error
145 function validate($ruleName, $values, $options = null, $multiple = false)
147 $rule =& $this->getRule($ruleName);
149 if (is_array($values) && !$multiple) {
151 foreach ($values as $value) {
152 if ($rule->validate($value, $options) === true) {
156 return ($result == 0) ? false : $result;
158 return $rule->validate($values, $options);
160 } // end func validate
163 * Returns the validation test in javascript code
165 * @param array|HTML_QuickForm_element Element(s) the rule applies to
166 * @param string Element name, in case $element is
168 * @param array Rule data
170 * @return string JavaScript for the rule
172 function getValidationScript(&$element, $elementName, $ruleData)
174 $reset = (isset($ruleData['reset'])) ? $ruleData['reset'] : false;
175 $rule =& $this->getRule($ruleData['type']);
176 if (!is_array($element)) {
177 list($jsValue, $jsReset) = $this->_getJsValue($element, $elementName, $reset, null);
179 $jsValue = " value = new Array();\n";
181 for ($i = 0; $i < count($element); $i++) {
182 list($tmp_value, $tmp_reset) = $this->_getJsValue($element[$i], $element[$i]->getName(), $reset, $i);
183 $jsValue .= "\n" . $tmp_value;
184 $jsReset .= $tmp_reset;
187 $jsField = isset($ruleData['group'])? $ruleData['group']: $elementName;
188 list ($jsPrefix, $jsCheck) = $rule->getValidationScript($ruleData['format']);
189 if (!isset($ruleData['howmany'])) {
190 $js = $jsValue . "\n" . $jsPrefix .
191 " if (" . str_replace('{jsVar}', 'value', $jsCheck) . " && !errFlag['{$jsField}']) {\n" .
192 " errFlag['{$jsField}'] = true;\n" .
193 " _qfMsg = _qfMsg + '\\n - {$ruleData['message']}';\n" .
197 $js = $jsValue . "\n" . $jsPrefix .
199 " for (var i = 0; i < value.length; i++) {\n" .
200 " if (!(" . str_replace('{jsVar}', 'value[i]', $jsCheck) . ")) {\n" .
204 " if (res < {$ruleData['howmany']} && !errFlag['{$jsField}']) {\n" .
205 " errFlag['{$jsField}'] = true;\n" .
206 " _qfMsg = _qfMsg + '\\n - {$ruleData['message']}';\n" .
211 } // end func getValidationScript
215 * Returns JavaScript to get and to reset the element's value
218 * @param HTML_QuickForm_element element being processed
219 * @param string element's name
220 * @param bool whether to generate JavaScript to reset
222 * @param integer value's index in the array (only used for
223 * multielement rules)
224 * @return array first item is value javascript, second is reset
226 function _getJsValue(&$element, $elementName, $reset = false, $index = null)
228 $jsIndex = isset($index)? '[' . $index . ']': '';
229 $tmp_reset = $reset? " var field = frm.elements['$elementName'];\n": '';
230 if (is_a($element, 'html_quickform_group')) {
231 $value = " _qfGroups['{$elementName}'] = {";
232 $elements =& $element->getElements();
233 for ($i = 0, $count = count($elements); $i < $count; $i++) {
234 $append = ($elements[$i]->getType() == 'select' && $elements[$i]->getMultiple())? '[]': '';
235 $value .= "'" . $element->getElementName($i) . $append . "': true" .
236 ($i < $count - 1? ', ': '');
240 " value{$jsIndex} = new Array();\n" .
241 " var valueIdx = 0;\n" .
242 " for (var i = 0; i < frm.elements.length; i++) {\n" .
243 " var _element = frm.elements[i];\n" .
244 " if (_element.name in _qfGroups['{$elementName}']) {\n" .
245 " switch (_element.type) {\n" .
246 " case 'checkbox':\n" .
248 " if (_element.checked) {\n" .
249 " value{$jsIndex}[valueIdx++] = _element.value;\n" .
252 " case 'select-one':\n" .
253 " if (-1 != _element.selectedIndex) {\n" .
254 " value{$jsIndex}[valueIdx++] = _element.options[_element.selectedIndex].value;\n" .
257 " case 'select-multiple':\n" .
258 " var tmpVal = new Array();\n" .
259 " var tmpIdx = 0;\n" .
260 " for (var j = 0; j < _element.options.length; j++) {\n" .
261 " if (_element.options[j].selected) {\n" .
262 " tmpVal[tmpIdx++] = _element.options[j].value;\n" .
265 " if (tmpIdx > 0) {\n" .
266 " value{$jsIndex}[valueIdx++] = tmpVal;\n" .
270 " value{$jsIndex}[valueIdx++] = _element.value;\n" .
276 " for (var i = 0; i < frm.elements.length; i++) {\n" .
277 " var _element = frm.elements[i];\n" .
278 " if (_element.name in _qfGroups['{$elementName}']) {\n" .
279 " switch (_element.type) {\n" .
280 " case 'checkbox':\n" .
282 " _element.checked = _element.defaultChecked;\n" .
284 " case 'select-one':\n" .
285 " case 'select-multiple':\n" .
286 " for (var j = 0; j < _element.options.length; j++) {\n" .
287 " _element.options[j].selected = _element.options[j].defaultSelected;\n" .
291 " _element.value = _element.defaultValue;\n" .
297 } elseif ($element->getType() == 'select') {
298 if ($element->getMultiple()) {
299 $elementName .= '[]';
301 " value{$jsIndex} = new Array();\n" .
302 " var valueIdx = 0;\n" .
303 " for (var i = 0; i < frm.elements['{$elementName}'].options.length; i++) {\n" .
304 " if (frm.elements['{$elementName}'].options[i].selected) {\n" .
305 " value{$jsIndex}[valueIdx++] = frm.elements['{$elementName}'].options[i].value;\n" .
309 $value = " value{$jsIndex} = frm.elements['{$elementName}'].selectedIndex == -1? '': frm.elements['{$elementName}'].options[frm.elements['{$elementName}'].selectedIndex].value;\n";
313 " for (var i = 0; i < field.options.length; i++) {\n" .
314 " field.options[i].selected = field.options[i].defaultSelected;\n" .
318 } elseif ($element->getType() == 'checkbox') {
319 if (is_a($element, 'html_quickform_advcheckbox')) {
320 $value = " value{$jsIndex} = frm.elements['$elementName'][1].checked? frm.elements['$elementName'][1].value: frm.elements['$elementName'][0].value;\n";
321 $tmp_reset .= $reset ? " field[1].checked = field[1].defaultChecked;\n" : '';
323 $value = " value{$jsIndex} = frm.elements['$elementName'].checked? '1': '';\n";
324 $tmp_reset .= $reset ? " field.checked = field.defaultChecked;\n" : '';
327 } elseif ($element->getType() == 'radio') {
328 $value = " value{$jsIndex} = '';\n" .
330 " var els = 'length' in frm.elements['$elementName']? frm.elements['$elementName']: [ frm.elements['$elementName'] ];\n" .
331 " for (var i = 0; i < els.length; i++) {\n" .
332 " if (els[i].checked) {\n" .
333 " value{$jsIndex} = els[i].value;\n" .
337 $tmp_reset .= " for (var i = 0; i < field.length; i++) {\n" .
338 " field[i].checked = field[i].defaultChecked;\n" .
343 $value = " value{$jsIndex} = frm.elements['$elementName'].value;";
344 $tmp_reset .= ($reset) ? " field.value = field.defaultValue;\n" : '';
346 return array($value, $tmp_reset);
348 } // end class HTML_QuickForm_RuleRegistry