Add support for "for" attributes in HTML forms - refs #7063

1.9.x
Yannick Warnier 11 years ago
parent e18d42ab7c
commit 0d7cf3b652
  1. 2
      main/inc/lib/formvalidator/FormValidator.class.php
  2. 7
      main/inc/lib/pear/HTML/QuickForm/Renderer/Default.php
  3. 26
      main/inc/lib/pear/HTML/QuickForm/element.php
  4. 7
      main/inc/lib/pear/HTML/QuickForm/label.php

@ -173,7 +173,7 @@ class FormValidator extends HTML_QuickForm
} else {
$element_template = '
<div class="control-group {error_class}">
<label class="control-label">
<label class="control-label" for="{label-for}">
<!-- BEGIN required --><span class="form_required">*</span><!-- END required -->
{label}
</label>

@ -241,7 +241,7 @@ class HTML_QuickForm_Renderer_Default extends HTML_QuickForm_Renderer
* @see renderElement()
* @return string Html for element
*/
function _prepareTemplate($name, $label, $required, $error)
function _prepareTemplate($name, $label, $required, $error, $labelFor = '')
{
if (is_array($label)) {
$nameLabel = array_shift($label);
@ -251,8 +251,10 @@ class HTML_QuickForm_Renderer_Default extends HTML_QuickForm_Renderer
if (isset($this->_templates[$name])) {
$html = str_replace('{label}', $nameLabel, $this->_templates[$name]);
$html = str_replace('{label-for}', $labelFor, $this->_templates[$name]);
} else {
$html = str_replace('{label}', $nameLabel, $this->_elementTemplate);
$html = str_replace('{label-for}', $labelFor, $this->_elementTemplate);
}
if ($required) {
$html = str_replace('<!-- BEGIN required -->', '', $html);
@ -295,10 +297,11 @@ class HTML_QuickForm_Renderer_Default extends HTML_QuickForm_Renderer
*/
function renderElement(&$element, $required, $error) {
if (!$this->_inGroup) {
$html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error);
$html = $this->_prepareTemplate($element->getName(), $element->getLabel(), $required, $error, $element->getLabelFor());
$this->_html .= str_replace('{element}', $element->toHtml(), $html);
} elseif (!empty($this->_groupElementTemplate)) {
$html = str_replace('{label}', $element->getLabel(), $this->_groupElementTemplate);
$html = str_replace('{label-for}', $element->getLabelFor(), $this->_groupElementTemplate);
if ($required) {
$html = str_replace('<!-- BEGIN required -->', '', $html);
$html = str_replace('<!-- END required -->', '', $html);

@ -52,6 +52,13 @@ class HTML_QuickForm_element extends HTML_Common
*/
var $_label = '';
/**
* Label "for" a field... (Chamilo LMS customization)
* @var string
* @access private
*/
var $_label_for = '';
/**
* Form element type
* @var string
@ -96,7 +103,7 @@ class HTML_QuickForm_element extends HTML_Common
$this->setName($elementName);
}
if (isset($elementLabel)) {
$this->setLabel($elementLabel);
$this->setLabel($elementLabel, $elementName);
}
} //end constructor
@ -307,13 +314,17 @@ class HTML_QuickForm_element extends HTML_Common
* Sets display text for the element
*
* @param string $label Display text for the element
* @param string $label_for Optionally add a "for" attribute
* @since 1.3
* @access public
* @return void
*/
function setLabel($label)
function setLabel($label, $labelFor = null)
{
$this->_label = $label;
if (!empty($labelFor)) {
$this->_label_for = $labelFor;
}
} //end func setLabel
// }}}
@ -331,6 +342,17 @@ class HTML_QuickForm_element extends HTML_Common
return $this->_label;
} //end func getLabel
/**
* Returns "for" attribute for the element
*
* @access public
* @return string
*/
function getLabelFor()
{
return $this->_label_for;
} //end func getLabelFor
// }}}
// {{{ _findValue()

@ -29,7 +29,7 @@ class HTML_QuickForm_label extends HTML_QuickForm_static
* @access public
* @return void
*/
function HTML_QuickForm_label($label = null, $text = null) {
function HTML_QuickForm_label($label = null, $text = null) {
$this->HTML_QuickForm_static(null, $label, $text);
$this->_type = 'html';
}
@ -48,9 +48,10 @@ class HTML_QuickForm_label extends HTML_QuickForm_static
$renderer->renderHtml($this);
}
function toHtml() {
function toHtml() {
$for = $this->getLabelFor();
return '<div class="control-group ">
<label class="control-label">'.$this->getLabel().'</label>
<label class="control-label"'.(empty($for)?'':' for="'.$for.'"').'>'.$this->getLabel().'</label>
<div class="controls">
'.HTML_QuickForm_static::toHtml().'
</div>

Loading…
Cancel
Save