QuickForm labels (again)

From: Date: Mon, 21 Apr 2003 17:28:45 +0000
Subject: QuickForm labels (again)
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-15277@lists.php.net to get a copy of this message
Attached is a first attempt at multiple labels, currently only with the Default renderer implemented. setLabel can be passed either a single value, or an array of values. addLabel can be used to add extra labels to an element. The default renderer will use the first label as the main label, and display the rest below the element, seperated by commas. Other renderers can do what they like with extra labels. Jon

? labels.diff ? QuickForm/Renderer/labels.diff 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 21 Apr 2003 17:30:44 -0000 @@ -41,7 +41,7 @@ * @since 1.3 * @access private */ - var $_label = ''; + var $_label = array(); /** * Form element type @@ -270,18 +270,40 @@ /** * Sets display text for the element * - * @param string $label Display text for a checkbox + * @param mixed $label Display text for a checkbox * @since 1.3 * @access public * @return void - * @throws + * @throws */ function setLabel($label) { + if (!is_array($label)) { + $label = array($label); + } $this->_label = $label; } //end func setLabel // }}} + // {{{ addLabel() + + /** + * Adds another label to the element + * + * @param mixed $label Extra label(s) + * @since 3.0 + * @access public + * @return void + * @throws + */ + function addLabel($label) + { + if (!is_array($label)) { + $label = array($label); + } + $this->_label = array_merge($this->_label, $label); + } // end func addLabel + // }}} // {{{ getLabel() /** @@ -416,4 +438,4 @@ // }}} } // end class HTML_QuickForm_element -?> \ No newline at end of file +?> 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 21 Apr 2003 17:30:45 -0000 @@ -50,8 +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 labels --><br/>{labels}<!-- END labels -->\n\t</td>\n\t</tr>"; /** * Form template string @@ -215,10 +215,11 @@ */ function _prepareTemplate($name, $label, $required, $error) { + $mainLabel = array_shift($label); if (isset($this->_templates[$name])) { - $html = str_replace('{label}', $label, $this->_templates[$name]); + $html = str_replace('{label}', $mainLabel, $this->_templates[$name]); } else { - $html = str_replace('{label}', $label, $this->_elementTemplate); + $html = str_replace('{label}', $mainLabel, $this->_elementTemplate); } if ($required) { $html = str_replace('<!-- BEGIN required -->', '', $html); @@ -233,7 +234,15 @@ } else { $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN error -->(\s|\S)*<!-- END error -->([ \t\n\r]*)?/i", '', $html); } - return $html; + + if (count($label)) { + $labels = implode(', ', $label); + $html = str_replace('{labels}', $labels, $html); + $html = str_replace('<!-- BEGIN labels -->', '', $html); + $html = str_replace('<!-- END labels -->', '', $html); + } else { + $html = preg_replace("/([ \t\n\r]*)?<!-- BEGIN labels -->(\s|\S)*<!-- END labels -->([ \t\n\r]*)?/i", '', $html); + } } // end func _prepareTemplate /**
« previous php.pear.dev (#15277) next »