📄 quickform.php
字号:
* @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);
break;
}
} while ($parent = get_parent_class($parent));
}
if ($ruleName) {
$registry =& HTML_QuickForm_RuleRegistry::singleton();
$registry->registerRule($ruleName, null, $name);
}
return $ruleName;
} // end func isRuleRegistered
// }}}
// {{{ getRegisteredRules()
/**
* Returns an array of registered validation rules
*
* @since 1.0
* @access public
* @return array
*/
function getRegisteredRules()
{
return array_keys($GLOBALS['_HTML_QuickForm_registered_rules']);
} // end func getRegisteredRules
// }}}
// {{{ isElementRequired()
/**
* Returns whether or not the form element is required
*
* @param string $element Form element name
* @since 1.0
* @access public
* @return boolean
*/
function isElementRequired($element)
{
return in_array($element, $this->_required, true);
} // end func isElementRequired
// }}}
// {{{ isElementFrozen()
/**
* Returns whether or not the form element is frozen
*
* @param string $element Form element name
* @since 1.0
* @access public
* @return boolean
*/
function isElementFrozen($element)
{
if (isset($this->_elementIndex[$element])) {
return $this->_elements[$this->_elementIndex[$element]]->isFrozen();
}
return false;
} // end func isElementFrozen
// }}}
// {{{ setJsWarnings()
/**
* Sets JavaScript warning messages
*
* @param string $pref Prefix warning
* @param string $post Postfix warning
* @since 1.1
* @access public
* @return void
*/
function setJsWarnings($pref, $post)
{
$this->_jsPrefix = $pref;
$this->_jsPostfix = $post;
} // end func setJsWarnings
// }}}
// {{{ setRequiredNote()
/**
* Sets required-note
*
* @param string $note Message indicating some elements are required
* @since 1.1
* @access public
* @return void
*/
function setRequiredNote($note)
{
$this->_requiredNote = $note;
} // end func setRequiredNote
// }}}
// {{{ getRequiredNote()
/**
* Returns the required note
*
* @since 2.0
* @access public
* @return string
*/
function getRequiredNote()
{
return $this->_requiredNote;
} // end func getRequiredNote
// }}}
// {{{ validate()
/**
* Performs the server side validation
* @access public
* @since 1.0
* @return boolean true if no error found
*/
function validate()
{
if (count($this->_rules) == 0 && count($this->_formRules) == 0 &&
$this->isSubmitted()) {
return true;
} elseif (!$this->isSubmitted()) {
return false;
}
include_once('HTML/QuickForm/RuleRegistry.php');
$registry =& HTML_QuickForm_RuleRegistry::singleton();
foreach ($this->_rules as $target => $rules) {
$submitValue = $this->getSubmitValue($target);
foreach ($rules as $elementName => $rule) {
if ((isset($rule['group']) && isset($this->_errors[$rule['group']])) ||
isset($this->_errors[$target])) {
continue 2;
}
// If element is not required and is empty, we shouldn't validate it
if (!$this->isElementRequired($target)) {
if (!isset($submitValue) || '' == $submitValue) {
continue 2;
// Fix for bug #3501: we shouldn't validate not uploaded files, either.
// Unfortunately, we can't just use $element->isUploadedFile() since
// the element in question can be buried in group. Thus this hack.
} elseif (is_array($submitValue)) {
if (false === ($pos = strpos($target, '['))) {
$isUpload = !empty($this->_submitFiles[$target]);
} else {
$base = substr($target, 0, $pos);
$idx = "['" . str_replace(array(']', '['), array('', "']['"), substr($target, $pos + 1, -1)) . "']";
eval("\$isUpload = isset(\$this->_submitFiles['{$base}']['name']{$idx});");
}
if ($isUpload && (!isset($submitValue['error']) || 0 != $submitValue['error'])) {
continue 2;
}
}
}
if (isset($rule['dependent']) && is_array($rule['dependent'])) {
$values = array($submitValue);
foreach ($rule['dependent'] as $elName) {
$values[] = $this->getSubmitValue($elName);
}
$result = $registry->validate($rule['type'], $values, $rule['format'], true);
} elseif (is_array($submitValue) && !isset($rule['howmany'])) {
$result = $registry->validate($rule['type'], $submitValue, $rule['format'], true);
} else {
$result = $registry->validate($rule['type'], $submitValue, $rule['format'], false);
}
if (!$result || (!empty($rule['howmany']) && $rule['howmany'] > (int)$result)) {
if (isset($rule['group'])) {
$this->_errors[$rule['group']] = $rule['message'];
} else {
$this->_errors[$target] = $rule['message'];
}
}
}
}
// process the global rules now
foreach ($this->_formRules as $rule) {
if (true !== ($res = call_user_func($rule, $this->_submitValues, $this->_submitFiles))) {
if (is_array($res)) {
$this->_errors += $res;
} else {
return PEAR::raiseError(null, QUICKFORM_ERROR, null, E_USER_WARNING, 'Form rule callback returned invalid value in HTML_QuickForm::validate()', 'HTML_QuickForm_Error', true);
}
}
}
return (0 == count($this->_errors));
} // end func validate
// }}}
// {{{ freeze()
/**
* Displays elements without HTML input tags
*
* @param mixed $elementList array or string of element(s) to be frozen
* @since 1.0
* @access public
* @throws HTML_QuickForm_Error
*/
function freeze($elementList=null)
{
if (!isset($elementList)) {
$this->_freezeAll = true;
$elementList = array();
} else {
if (!is_array($elementList)) {
$elementList = preg_split('/[ ]*,[ ]*/', $elementList);
}
$elementList = array_flip($elementList);
}
foreach (array_keys($this->_elements) as $key) {
$name = $this->_elements[$key]->getName();
if ($this->_freezeAll || isset($elementList[$name])) {
$this->_elements[$key]->freeze();
unset($elementList[$name]);
}
}
if (!empty($elementList)) {
return PEAR::raiseError(null, QUICKFORM_NONEXIST_ELEMENT, null, E_USER_WARNING, "Nonexistant element(s): '" . implode("', '", array_keys($elementList)) . "' in HTML_QuickForm::freeze()", 'HTML_QuickForm_Error', true);
}
return true;
} // end func freeze
// }}}
// {{{ isFrozen()
/**
* Returns whether or not the whole form is frozen
*
* @since 3.0
* @access public
* @return boolean
*/
function isFrozen()
{
return $this->_freezeAll;
} // end func isFrozen
// }}}
// {{{ process()
/**
* Performs the form data processing
*
* @param mixed $callback Callback, either function name or array(&$object, 'method')
* @param bool $mergeFiles Whether uploaded files should be processed too
* @since 1.0
* @access public
* @throws HTML_QuickForm_Error
*/
function process($callback, $mergeFiles = true)
{
if (!is_callable($callback)) {
return PEAR::raiseError(null, QUICKFORM_INVALID_PROCESS, null, E_USER_WARNING, "Callback function does not exist in QuickForm::process()", 'HTML_QuickForm_Error', true);
}
$values = ($mergeFiles === true) ? HTML_QuickForm::arrayMerge($this->_submitValues, $this->_submitFiles) : $this->_submitValues;
return call_user_func($callback, $values);
} // end func process
// }}}
// {{{ accept()
/**
* Accepts a renderer
*
* @param object An HTML_QuickForm_Renderer object
* @since 3.0
* @access public
* @return void
*/
function accept(&$renderer)
{
$renderer->startForm($this);
foreach (array_keys($this->_elements) as $key) {
$element =& $this->_elements[$key];
$elementName = $element->getName();
$required = ($this->isElementRequired($elementName) && !$element->isFrozen());
$error = $this->getElementError($elementName);
$element->accept($renderer, $required, $error);
}
$renderer->finishForm($this);
} // end func accept
// }}}
// {{{ defaultRenderer()
/**
* Returns a reference to default renderer object
*
* @access public
* @since 3.0
* @return object a default renderer object
*/
function &defaultRenderer()
{
if (!isset($GLOBALS['_HTML_QuickForm_default_renderer'])) {
include_once('HTML/QuickForm/Renderer/Default.php');
$GLOBALS['_HTML_QuickForm_default_renderer'] =& new HTML_QuickForm_Renderer_Default();
}
return $GLOBALS['_HTML_QuickForm_default_renderer'];
} // e
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -