📄 quickform.php
字号:
* * If the element is in fact a group, it will be considered as a whole. * To validate grouped elements as separated entities, * use addGroupRule instead of addRule. * * @param string $element Form element name * @param string $message Message to display for invalid data * @param string $type Rule type, use getRegisteredRules() to get types * @param string $format (optional)Required for extra rule data * @param string $validation (optional)Where to perform validation: "server", "client" * @param boolean $reset Client-side validation: reset the form element to its original value if there is an error? * @param boolean $force Force the rule to be applied, even if the target form element does not exist * @since 1.0 * @access public * @throws HTML_QuickForm_Error */ function addRule($element, $message, $type, $format=null, $validation='server', $reset = false, $force = false) { if (!$force) { if (!is_array($element) && !$this->elementExists($element)) { return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$element' does not exist in HTML_QuickForm::addRule()", 'HTML_QuickForm_Error', true); } elseif (is_array($element)) { foreach ($element as $el) { if (!$this->elementExists($el)) { return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Element '$el' does not exist in HTML_QuickForm::addRule()", 'HTML_QuickForm_Error', true); } } } } if (false === ($newName = $this->isRuleRegistered($type, true))) { return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, "Rule '$type' is not registered in HTML_QuickForm::addRule()", 'HTML_QuickForm_Error', true); } elseif (is_string($newName)) { $type = $newName; } if (is_array($element)) { $dependent = $element; $element = array_shift($dependent); } else { $dependent = null; } if ($type == 'required' || $type == 'uploadedfile') { $this->_required[] = $element; } if (!isset($this->_rules[$element])) { $this->_rules[$element] = array(); } if ($validation == 'client') { $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);')); } $this->_rules[$element][] = array( 'type' => $type, 'format' => $format, 'message' => $message, 'validation' => $validation, 'reset' => $reset, 'dependent' => $dependent ); } // end func addRule // }}} // {{{ addGroupRule() /** * Adds a validation rule for the given group of elements * * Only groups with a name can be assigned a validation rule * Use addGroupRule when you need to validate elements inside the group. * Use addRule if you need to validate the group as a whole. In this case, * the same rule will be applied to all elements in the group. * Use addRule if you need to validate the group against a function. * * @param string $group Form group name * @param mixed $arg1 Array for multiple elements or error message string for one element * @param string $type (optional)Rule type use getRegisteredRules() to get types * @param string $format (optional)Required for extra rule data * @param int $howmany (optional)How many valid elements should be in the group * @param string $validation (optional)Where to perform validation: "server", "client" * @param bool $reset Client-side: whether to reset the element's value to its original state if validation failed. * @since 2.5 * @access public * @throws HTML_QuickForm_Error */ function addGroupRule($group, $arg1, $type='', $format=null, $howmany=0, $validation = 'server', $reset = false) { if (!$this->elementExists($group)) { return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Group '$group' does not exist in HTML_QuickForm::addGroupRule()", 'HTML_QuickForm_Error', true); } $groupObj =& $this->getElement($group); if (is_array($arg1)) { $required = 0; foreach ($arg1 as $elementIndex => $rules) { $elementName = $groupObj->getElementName($elementIndex); foreach ($rules as $rule) { $format = (isset($rule[2])) ? $rule[2] : null; $validation = (isset($rule[3]) && 'client' == $rule[3])? 'client': 'server'; $reset = isset($rule[4]) && $rule[4]; $type = $rule[1]; if (false === ($newName = $this->isRuleRegistered($type, true))) { return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, "Rule '$type' is not registered in HTML_QuickForm::addGroupRule()", 'HTML_QuickForm_Error', true); } elseif (is_string($newName)) { $type = $newName; } $this->_rules[$elementName][] = array( 'type' => $type, 'format' => $format, 'message' => $rule[0], 'validation' => $validation, 'reset' => $reset, 'group' => $group); if ('required' == $type || 'uploadedfile' == $type) { $groupObj->_required[] = $elementName; $this->_required[] = $elementName; $required++; } if ('client' == $validation) { $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);')); } } } if ($required > 0 && count($groupObj->getElements()) == $required) { $this->_required[] = $group; } } elseif (is_string($arg1)) { if (false === ($newName = $this->isRuleRegistered($type, true))) { return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, "Rule '$type' is not registered in HTML_QuickForm::addGroupRule()", 'HTML_QuickForm_Error', true); } elseif (is_string($newName)) { $type = $newName; } // addGroupRule() should also handle <select multiple> if (is_a($groupObj, 'html_quickform_group')) { // Radios need to be handled differently when required if ($type == 'required' && $groupObj->getGroupType() == 'radio') { $howmany = ($howmany == 0) ? 1 : $howmany; } else { $howmany = ($howmany == 0) ? count($groupObj->getElements()) : $howmany; } } $this->_rules[$group][] = array('type' => $type, 'format' => $format, 'message' => $arg1, 'validation' => $validation, 'howmany' => $howmany, 'reset' => $reset); if ($type == 'required') { $this->_required[] = $group; } if ($validation == 'client') { $this->updateAttributes(array('onsubmit' => 'try { var myValidator = validate_' . $this->_attributes['id'] . '; } catch(e) { return true; } return myValidator(this);')); } } } // end func addGroupRule // }}} // {{{ addFormRule() /** * Adds a global validation rule * * This should be used when for a rule involving several fields or if * you want to use some completely custom validation for your form. * The rule function/method should return true in case of successful * validation and array('element name' => 'error') when there were errors. * * @access public * @param mixed Callback, either function name or array(&$object, 'method') * @throws HTML_QuickForm_Error */ function addFormRule($rule) { if (!is_callable($rule)) { return PEAR::raiseError(null, QUICKFORM_INVALID_RULE, null, E_USER_WARNING, 'Callback function does not exist in HTML_QuickForm::addFormRule()', 'HTML_QuickForm_Error', true); } $this->_formRules[] = $rule; } // }}} // {{{ applyFilter() /** * Applies a data filter for the given field(s) * * @param mixed $element Form element name or array of such names * @param mixed $filter Callback, either function name or array(&$object, 'method') * @since 2.0 * @access public */ function applyFilter($element, $filter) { if (!is_callable($filter)) { return PEAR::raiseError(null, QUICKFORM_INVALID_FILTER, null, E_USER_WARNING, "Callback function does not exist in QuickForm::applyFilter()", 'HTML_QuickForm_Error', true); } if ($element == '__ALL__') { $this->_submitValues = $this->_recursiveFilter($filter, $this->_submitValues); } else { if (!is_array($element)) { $element = array($element); } foreach ($element as $elName) { $value = $this->getSubmitValue($elName); if (null !== $value) { if (false === strpos($elName, '[')) { $this->_submitValues[$elName] = $this->_recursiveFilter($filter, $value); } else { $idx = "['" . str_replace( array('\\', '\'', ']', '['), array('\\\\', '\\\'', '', "']['"), $elName ) . "']"; eval("\$this->_submitValues{$idx} = \$this->_recursiveFilter(\$filter, \$value);"); } } } } } // end func applyFilter // }}} // {{{ _recursiveFilter() /** * Recursively apply a filter function * * @param string $filter filter to apply * @param mixed $value submitted values * @since 2.0 * @access private * @return cleaned values */ function _recursiveFilter($filter, $value) { if (is_array($value)) { $cleanValues = array(); foreach ($value as $k => $v) { $cleanValues[$k] = $this->_recursiveFilter($filter, $v); } return $cleanValues; } else { return call_user_func($filter, $value); } } // end func _recursiveFilter // }}} // {{{ arrayMerge() /** * Merges two arrays * * Merges two array like the PHP function array_merge but recursively. * The main difference is that existing keys will not be renumbered * if they are integers. * * @access puplic * @param array $a original array * @param array $b array which will be merged into first one * @return array merged array */ function arrayMerge($a, $b) { foreach ($b as $k => $v) { if (is_array($v)) { if (isset($a[$k]) && !is_array($a[$k])) { $a[$k] = $v; } else { if (!isset($a[$k])) { $a[$k] = array(); } $a[$k] = HTML_QuickForm::arrayMerge($a[$k], $v); } } else { $a[$k] = $v; } } return $a; } // end func arrayMerge // }}} // {{{ isTypeRegistered() /** * Returns whether or not the form element type is supported * * @param string $type Form element type * @since 1.0 * @access public * @return boolean */ function isTypeRegistered($type) { return isset($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES'][strtolower($type)]); } // end func isTypeRegistered // }}} // {{{ getRegisteredTypes() /** * Returns an array of registered element types * * @since 1.0 * @access public * @return array */ function getRegisteredTypes() { return array_keys($GLOBALS['HTML_QUICKFORM_ELEMENT_TYPES']); } // end func getRegisteredTypes // }}} // {{{ isRuleRegistered() /** * Returns whether or not the given rule is supported * * @param string $name Validation rule name * @param bool Whether to automatically register subclasses of HTML_QuickForm_Rule * @since 1.0 * @access public * @return mixed true if previously registered, false if not, new rule name if auto-registering worked */ function isRuleRegistered($name, $autoRegister = false) { if (is_scalar($name) && isset($GLOBALS['_HTML_QuickForm_registered_rules'][$name])) { return true; } elseif (!$autoRegister) { return false; } // automatically register the rule if requested include_once 'HTML/QuickForm/RuleRegistry.php'; $ruleName = false; if (is_object($name) && is_a($name, 'html_quickform_rule')) { $ruleName = !empty($name->name)? $name->name: strtolower(get_class($name)); } elseif (is_string($name) && class_exists($name)) { $parent = strtolower($name); do { if ('html_quickform_rule' == strtolower($parent)) { $ruleName = strtolower($name);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -