2 /* vim: set expandtab tabstop=4 shiftwidth=4: */
3 // +----------------------------------------------------------------------+
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: Adam Daniel <adaniel1@eesus.jnj.com> |
17 // | Alexey Borzov <borz_off@cs.msu.su> |
18 // | Bertrand Mansion <bmansion@mamasam.com> |
19 // +----------------------------------------------------------------------+
24 * Registers rule objects and uses them for validation
27 class HTML_QuickForm_RuleRegistry
30 * Array containing references to used rules
34 var $_rules = array();
38 * Returns a singleton of HTML_QuickForm_RuleRegistry
40 * Usually, only one RuleRegistry object is needed, this is the reason
41 * why it is recommended to use this method to get the validation object.
45 * @return object Reference to the HTML_QuickForm_RuleRegistry singleton
51 $obj = new HTML_QuickForm_RuleRegistry();
54 } // end func singleton
57 * Registers a new validation rule
59 * In order to use a custom rule in your form, you need to register it
60 * first. For regular expressions, one can directly use the 'regex' type
61 * rule in addRule(), this is faster than registering the rule.
63 * Functions and methods can be registered. Use the 'function' type.
64 * When registering a method, specify the class name as second parameter.
66 * You can also register an HTML_QuickForm_Rule subclass with its own
69 * @param string $ruleName Name of validation rule
70 * @param string $type Either: 'regex', 'function' or null
71 * @param string $data1 Name of function, regular expression or
72 * HTML_QuickForm_Rule object class name
73 * @param string $data2 Object parent of above function or HTML_QuickForm_Rule file path
77 function registerRule($ruleName, $type, $data1, $data2 = null)
79 $type = strtolower($type);
80 if ($type == 'regex') {
82 $rule =& $this->getRule('regex');
83 $rule->addData($ruleName, $data1);
84 $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = $GLOBALS['_HTML_QuickForm_registered_rules']['regex'];
86 } elseif ($type == 'function' ||
$type == 'callback') {
88 $rule =& $this->getRule('callback');
89 $rule->addData($ruleName, $data1, $data2, 'function' == $type);
90 $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = $GLOBALS['_HTML_QuickForm_registered_rules']['callback'];
92 } elseif (is_object($data1)) {
93 // An instance of HTML_QuickForm_Rule
94 $this->_rules
[strtolower(get_class($data1))] = $data1;
95 $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = array(strtolower(get_class($data1)), null);
99 $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName] = array(strtolower($data1), $data2);
101 } // end func registerRule
104 * Returns a reference to the requested rule object
106 * @param string $ruleName Name of the requested rule
110 function &getRule($ruleName)
112 list($class, $path) = $GLOBALS['_HTML_QuickForm_registered_rules'][$ruleName];
114 if (!isset($this->_rules
[$class])) {
118 $this->_rules
[$class] =& new $class();
120 $this->_rules
[$class]->setName($ruleName);
121 return $this->_rules
[$class];
122 } // end func getRule
125 * Performs validation on the given values
127 * @param string $ruleName Name of the rule to be used
128 * @param mixed $values Can be a scalar or an array of values
130 * @param mixed $options Options used by the rule
131 * @param mixed $multiple Whether to validate an array of values altogether
133 * @return mixed true if no error found, int of valid values (when an array of values is given) or false if error
135 function validate($ruleName, $values, $options = null, $multiple = false)
137 $rule =& $this->getRule($ruleName);
139 if (is_array($values) && !$multiple) {
141 foreach ($values as $value) {
142 if ($rule->validate($value, $options) === true) {
146 return ($result == 0) ?
false : $result;
148 return $rule->validate($values, $options);
150 } // end func validate
153 * Returns the validation test in javascript code
155 * @param mixed Element(s) the rule applies to
156 * @param string Element name, in case $element is not array
157 * @param array Rule data
159 * @return string JavaScript for the rule
161 function getValidationScript(&$element, $elementName, $ruleData)
163 $reset = (isset($ruleData['reset'])) ?
$ruleData['reset'] : false;
164 $rule =& $this->getRule($ruleData['type']);
165 if (!is_array($element)) {
166 list($jsValue, $jsReset) = $this->_getJsValue($element, $elementName, $reset, null);
168 $jsValue = " value = new Array();\n";
170 for ($i = 0; $i < count($element); $i++
) {
171 list($tmp_value, $tmp_reset) = $this->_getJsValue($element[$i], $element[$i]->getName(), $reset, $i);
172 $jsValue .= "\n" . $tmp_value;
173 $jsReset .= $tmp_reset;
176 $jsField = isset($ruleData['group'])?
$ruleData['group']: $elementName;
177 list ($jsPrefix, $jsCheck) = $rule->getValidationScript($ruleData['format']);
178 if (!isset($ruleData['howmany'])) {
179 $js = $jsValue . "\n" . $jsPrefix .
180 " if (" . str_replace('{jsVar}', 'value', $jsCheck) . " && !errFlag['{$jsField}']) {\n" .
181 " errFlag['{$jsField}'] = true;\n" .
182 " _qfMsg = _qfMsg + '\\n - {$ruleData['message']}';\n" .
186 $js = $jsValue . "\n" . $jsPrefix .
188 " for (var i = 0; i < value.length; i++) {\n" .
189 " if (!(" . str_replace('{jsVar}', 'value[i]', $jsCheck) . ")) {\n" .
193 " if (res < {$ruleData['howmany']} && !errFlag['{$jsField}']) {\n" .
194 " errFlag['{$jsField}'] = true;\n" .
195 " _qfMsg = _qfMsg + '\\n - {$ruleData['message']}';\n" .
200 } // end func getValidationScript
204 * Returns JavaScript to get and to reset the element's value
207 * @param object HTML_QuickForm_element element being processed
208 * @param string element's name
209 * @param bool whether to generate JavaScript to reset the value
210 * @param integer value's index in the array (only used for multielement rules)
211 * @return array first item is value javascript, second is reset
213 function _getJsValue(&$element, $elementName, $reset = false, $index = null)
215 $jsIndex = isset($index)?
'[' . $index . ']': '';
216 $tmp_reset = $reset?
" var field = frm.elements['$elementName'];\n": '';
217 if (is_a($element, 'html_quickform_group')) {
218 $value = " _qfGroups['{$elementName}'] = {";
219 $elements =& $element->getElements();
220 for ($i = 0, $count = count($elements); $i < $count; $i++
) {
221 $append = ($elements[$i]->getType() == 'select' && $elements[$i]->getMultiple())?
'[]': '';
222 $value .= "'" . $element->getElementName($i) . $append . "': true" .
223 ($i < $count - 1?
', ': '');
227 " value{$jsIndex} = new Array();\n" .
228 " var valueIdx = 0;\n" .
229 " for (var i = 0; i < frm.elements.length; i++) {\n" .
230 " var _element = frm.elements[i];\n" .
231 " if (_element.name in _qfGroups['{$elementName}']) {\n" .
232 " switch (_element.type) {\n" .
233 " case 'checkbox':\n" .
235 " if (_element.checked) {\n" .
236 " value{$jsIndex}[valueIdx++] = _element.value;\n" .
239 " case 'select-one':\n" .
240 " if (-1 != _element.selectedIndex) {\n" .
241 " value{$jsIndex}[valueIdx++] = _element.options[_element.selectedIndex].value;\n" .
244 " case 'select-multiple':\n" .
245 " var tmpVal = new Array();\n" .
246 " var tmpIdx = 0;\n" .
247 " for (var j = 0; j < _element.options.length; j++) {\n" .
248 " if (_element.options[j].selected) {\n" .
249 " tmpVal[tmpIdx++] = _element.options[j].value;\n" .
252 " if (tmpIdx > 0) {\n" .
253 " value{$jsIndex}[valueIdx++] = tmpVal;\n" .
257 " value{$jsIndex}[valueIdx++] = _element.value;\n" .
263 " for (var i = 0; i < frm.elements.length; i++) {\n" .
264 " var _element = frm.elements[i];\n" .
265 " if (_element.name in _qfGroups['{$elementName}']) {\n" .
266 " switch (_element.type) {\n" .
267 " case 'checkbox':\n" .
269 " _element.checked = _element.defaultChecked;\n" .
271 " case 'select-one':\n" .
272 " case 'select-multiple':\n" .
273 " for (var j = 0; j < _element.options.length; j++) {\n" .
274 " _element.options[j].selected = _element.options[j].defaultSelected;\n" .
278 " _element.value = _element.defaultValue;\n" .
284 } elseif ($element->getType() == 'select') {
285 if ($element->getMultiple()) {
286 $elementName .= '[]';
288 " value{$jsIndex} = new Array();\n" .
289 " var valueIdx = 0;\n" .
290 " for (var i = 0; i < frm.elements['{$elementName}'].options.length; i++) {\n" .
291 " if (frm.elements['{$elementName}'].options[i].selected) {\n" .
292 " value{$jsIndex}[valueIdx++] = frm.elements['{$elementName}'].options[i].value;\n" .
296 $value = " value{$jsIndex} = frm.elements['{$elementName}'].selectedIndex == -1? '': frm.elements['{$elementName}'].options[frm.elements['{$elementName}'].selectedIndex].value;\n";
300 " for (var i = 0; i < field.options.length; i++) {\n" .
301 " field.options[i].selected = field.options[i].defaultSelected;\n" .
305 } elseif ($element->getType() == 'checkbox') {
306 if (is_a($element, 'html_quickform_advcheckbox')) {
307 $value = " value{$jsIndex} = frm.elements['$elementName'][1].checked? frm.elements['$elementName'][1].value: frm.elements['$elementName'][0].value;\n";
308 $tmp_reset .= $reset ?
" field[1].checked = field[1].defaultChecked;\n" : '';
310 $value = " value{$jsIndex} = frm.elements['$elementName'].checked? '1': '';\n";
311 $tmp_reset .= $reset ?
" field.checked = field.defaultChecked;\n" : '';
314 } elseif ($element->getType() == 'radio') {
315 $value = " value{$jsIndex} = '';\n" .
317 " var els = 'length' in frm.elements['$elementName']? frm.elements['$elementName']: [ frm.elements['$elementName'] ];\n" .
318 " for (var i = 0; i < els.length; i++) {\n" .
319 " if (els[i].checked) {\n" .
320 " value{$jsIndex} = els[i].value;\n" .
324 $tmp_reset .= " for (var i = 0; i < field.length; i++) {\n" .
325 " field[i].checked = field[i].defaultChecked;\n" .
330 $value = " value{$jsIndex} = frm.elements['$elementName'].value;";
331 $tmp_reset .= ($reset) ?
" field.value = field.defaultValue;\n" : '';
333 return array($value, $tmp_reset);
335 } // end class HTML_QuickForm_RuleRegistry