QuickForm Patch

From: Date: Sun, 20 Apr 2003 14:38:39 +0000
Subject: QuickForm Patch
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-15257@lists.php.net to get a copy of this message
After making a complete mess of submitting this the other day, I thought I'd submit it again. This adds a setElementDescription method to the main QuickForm class to add some instructional text to an element, and adds setValue to the static elements so they can be swapped with other elements. Jon

? setDescription.patch Index: QuickForm.php =================================================================== RCS file: /repository/pear/HTML_QuickForm/QuickForm.php,v retrieving revision 1.87 diff -u -r1.87 QuickForm.php --- QuickForm.php 17 Apr 2003 15:54:13 -0000 1.87 +++ QuickForm.php 18 Apr 2003 16:40:05 -0000 @@ -197,6 +197,14 @@ var $_errors = array(); /** + * Array containing the element descriptions + * @since 1.0 + * @var array + * @access private + */ + var $_descriptions = array(); + + /** * Note for required fields in the form * @var string * @since 1.0 @@ -710,6 +718,39 @@ } // end func setElementError // }}} + // {{{ getElementDescription() + + /** + * Returns an element's description text + * + * @param string $element Name of form element to check + * @since 2.10 + * @access public + * @return string description of the checked element + */ + function getElementDescription($element) + { + if (isset($this->_descriptions[$element])) { + return $this->_descriptions[$element]; + } + } // end func getElementDescription + // }} + // {{{ setElementDescription() + + /** + * Set some description text for a form element + * + * @param string $element Name of form element + * @param string $description Description text + * @since 2.10 + * @access public + * @return void + */ + function setElementDescription($element,$description) + { + $this->_descriptions[$element] = $description; + } // end func setElementDescription + // }}} // {{{ getElementType() /** @@ -1647,7 +1688,8 @@ $elementName = $element->getName(); $required = ($this->isElementRequired($elementName) && $this->_freezeAll == false); $error = $this->getElementError($elementName); - $element->accept($renderer, $required, $error); + $description = $this->getElementDescription($elementName); + $element->accept($renderer, $required, $error, $description); } $renderer->finishForm($this); } // end func accept @@ -1953,4 +1995,4 @@ // }}} } // end class HTML_QuickForm_Error -?> \ No newline at end of file +?> Index: QuickForm/Renderer.php =================================================================== RCS file: /repository/pear/HTML_QuickForm/QuickForm/Renderer.php,v retrieving revision 1.3 diff -u -r1.3 Renderer.php --- QuickForm/Renderer.php 12 Mar 2003 11:21:12 -0000 1.3 +++ QuickForm/Renderer.php 18 Apr 2003 16:40:06 -0000 @@ -86,7 +86,7 @@ * @return void * @abstract */ - function renderElement(&$element, $required, $error) + function renderElement(&$element, $required, $error, $description) { return; } // end func renderElement Index: QuickForm/element.php =================================================================== RCS file: /repository/pear/HTML_QuickForm/QuickForm/element.php,v retrieving revision 1.23 diff -u -r1.23 element.php --- QuickForm/element.php 10 Apr 2003 15:28:50 -0000 1.23 +++ QuickForm/element.php 18 Apr 2003 16:40:06 -0000 @@ -384,12 +384,13 @@ * @param object An HTML_QuickForm_Renderer object * @param bool Whether an element is required * @param string An error message associated with an element + * @param string A description associated with an element * @access public * @return void */ - function accept(&$renderer, $required=false, $error=null) + function accept(&$renderer, $required=false, $error=null, $description=null) { - $renderer->renderElement($this, $required, $error); + $renderer->renderElement($this, $required, $error, $description); } // end func accept // }}} @@ -416,4 +417,4 @@ // }}} } // end class HTML_QuickForm_element -?> \ No newline at end of file +?> Index: QuickForm/static.php =================================================================== RCS file: /repository/pear/HTML_QuickForm/QuickForm/static.php,v retrieving revision 1.2 diff -u -r1.2 static.php --- QuickForm/static.php 12 Mar 2003 11:13:07 -0000 1.2 +++ QuickForm/static.php 18 Apr 2003 16:40:06 -0000 @@ -102,6 +102,21 @@ } // end func setText // }}} + // {{{ setValue() + + /** + * Sets the text (uses the standard setValue call to emulate a form element. + * + * @param string $text + * @access public + * @return void + */ + function setValue($text) + { + $this->setText($text); + } // end func setValue + + // }}} // {{{ toHtml() /** Index: QuickForm/Renderer/Array.php =================================================================== RCS file: /repository/pear/HTML_QuickForm/QuickForm/Renderer/Array.php,v retrieving revision 1.4 diff -u -r1.4 Array.php --- QuickForm/Renderer/Array.php 17 Apr 2003 15:42:05 -0000 1.4 +++ QuickForm/Renderer/Array.php 18 Apr 2003 16:40:06 -0000 @@ -159,11 +159,14 @@ } // end func renderHeader - function renderElement(&$element, $required, $error) + function renderElement(&$element, $required, $error, $description) { $elAry = $this->_elementToArray($element, $required, $error); if (!empty($error)) { $this->_ary['errors'][$elAry['name']] = $error; + } + if (!empty($description)) { + $this->_ary['descriptions'][$elAry['description']] = $description; } $this->_storeArray($elAry); } // end func renderElement Index: QuickForm/Renderer/Default.php =================================================================== RCS file: /repository/pear/HTML_QuickForm/QuickForm/Renderer/Default.php,v retrieving revision 1.7 diff -u -r1.7 Default.php --- QuickForm/Renderer/Default.php 30 Mar 2003 13:41:31 -0000 1.7 +++ QuickForm/Renderer/Default.php 18 Apr 2003 16:40:07 -0000 @@ -50,9 +50,8 @@ * @var string * @access private */ - var $_elementTemplate = - "\n\t<tr>\n\t\t<td align=\"right\" valign=\"top\"><!-- BEGIN required --><font color=\"red\">*</font><!-- END required --><b>{label}</b></td>\n\t\t<td valign=\"top\" align=\"left\"><!-- BEGIN error --><font color=\"#FF0000\">{error}</font><br /><!-- END error -->\t{element}</td>\n\t</tr>"; - + var $_elementTemplate = + "\n\t<tr>\n\t\t<td align=\"right\" valign=\"top\"><!-- BEGIN required --><font color=\"red\">*</font><!-- END required --><b>{label}</b></td>\n\t\t<td nowrap=\"nowrap\" valign=\"top\" align=\"left\"><!-- BEGIN error --><font color=\"#FF0000\">{error}</font><br><!-- END error -->\t{element}<!-- BEGIN description --><br>{description}<!-- END description -->\n\t</td>\n\t</tr>"; /** * Form template string * @var string @@ -209,11 +208,12 @@ * @param string Element label * @param bool Whether an element is required * @param string Error message associated with the element + * @param description Description associated with the element * @access private * @see renderElement() * @return string Html for element */ - function _prepareTemplate($name, $label, $required, $error) + function _prepareTemplate($name, $label, $required, $error, $description) { if (isset($this->_templates[$name])) { $html = str_replace('{label}', $label, $this->_templates[$name]); @@ -233,6 +233,13 @@ } else { $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->(\s|\S)*<!-- END error -->([ \t\n\r]*)?/i", '', $html); } + if (isset($description)) { + $html = str_replace('{description}', $description, $html); + $html = str_replace('<!-- BEGIN description -->', '', $html); + $html = str_replace('<!-- END description -->', '', $html); + } else { + $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN description -->(\s|\S)*<!-- END description -->([ \t\n\r]*)?/i", '', $html); + } return $html; } // end func _prepareTemplate @@ -243,13 +250,14 @@ * @param object An HTML_QuickForm_element object being visited * @param bool Whether an element is required * @param string An error message associated with an element + * @param string A description associated with an element * @access public * @return void */ - function renderElement(&$element, $required, $error) + function renderElement(&$element, $required, $error, $description) { if (!$this->_inGroup) { - $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error); + $html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error, $description); $this->_html .= str_replace('{element}', $element->toHtml(), $html); } elseif (!empty($this->_groupElementTemplate)) { Index: QuickForm/Renderer/ITDynamic.php =================================================================== RCS file: /repository/pear/HTML_QuickForm/QuickForm/Renderer/ITDynamic.php,v retrieving revision 1.1 diff -u -r1.1 ITDynamic.php --- QuickForm/Renderer/ITDynamic.php 9 Apr 2003 11:10:24 -0000 1.1 +++ QuickForm/Renderer/ITDynamic.php 18 Apr 2003 16:40:07 -0000 @@ -124,7 +124,7 @@ } - function renderElement(&$element, $required, $error) + function renderElement(&$element, $required, $error, $description) { $blockName = $this->_matchBlock($element); // are we inside a group? Index: QuickForm/Renderer/ITStatic.php =================================================================== RCS file: /repository/pear/HTML_QuickForm/QuickForm/Renderer/ITStatic.php,v retrieving revision 1.1 diff -u -r1.1 ITStatic.php --- QuickForm/Renderer/ITStatic.php 10 Apr 2003 21:23:53 -0000 1.1 +++ QuickForm/Renderer/ITStatic.php 18 Apr 2003 16:40:08 -0000 @@ -168,10 +168,11 @@ * @param object An HTML_QuickForm_element object being visited * @param bool Whether an element is required * @param string An error message associated with an element + * @param string A description associated with an element * @access public * @return void */ - function renderElement(&$element, $required, $error) + function renderElement(&$element, $required, $error, $description) { $name = $element->getName(); @@ -384,4 +385,4 @@ } }// end func _renderError } // end class HTML_QuickForm_Renderer_ITStatic -?> \ No newline at end of file +?>
« previous php.pear.dev (#15257) next »