From 86aab05ced2e926f52735cad7d786b39abc69350 Mon Sep 17 00:00:00 2001 From: jamiesensei Date: Mon, 8 Jan 2007 15:53:17 +0000 Subject: [PATCH] fix for xhtml probs MDL-8102 and MDL-7979 - invalid id attributes. and stopped using label tag for a group of elements where the label refers to the group of elements and not an individual one. --- lib/form/advcheckbox.php | 2 +- lib/form/checkbox.php | 2 +- lib/form/radio.php | 2 +- lib/formslib.php | 35 +++++++++++++++++++---------------- 4 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/form/advcheckbox.php b/lib/form/advcheckbox.php index df51110fe1..6a3ed1c4cd 100644 --- a/lib/form/advcheckbox.php +++ b/lib/form/advcheckbox.php @@ -59,7 +59,7 @@ class MoodleQuickForm_advcheckbox extends HTML_QuickForm_advcheckbox{ static $idx = 1; if (!$this->getAttribute('id')) { - $this->updateAttributes(array('id' => substr(md5(microtime() . $idx++), 0, 6))); + $this->updateAttributes(array('id' => 'id_'.substr(md5(microtime() . $idx++), 0, 6))); } } // end func _generateId diff --git a/lib/form/checkbox.php b/lib/form/checkbox.php index 6308f4ab2b..b62861b1de 100644 --- a/lib/form/checkbox.php +++ b/lib/form/checkbox.php @@ -59,7 +59,7 @@ class MoodleQuickForm_checkbox extends HTML_QuickForm_checkbox{ static $idx = 1; if (!$this->getAttribute('id')) { - $this->updateAttributes(array('id' => substr(md5(microtime() . $idx++), 0, 6))); + $this->updateAttributes(array('id' => 'id_'.substr(md5(microtime() . $idx++), 0, 6))); } } // end func _generateId /** diff --git a/lib/form/radio.php b/lib/form/radio.php index 2fb2cb5798..9d305ee6a7 100644 --- a/lib/form/radio.php +++ b/lib/form/radio.php @@ -59,7 +59,7 @@ class MoodleQuickForm_radio extends HTML_QuickForm_radio{ static $idx = 1; if (!$this->getAttribute('id')) { - $this->updateAttributes(array('id' => substr(md5(microtime() . $idx++), 0, 6))); + $this->updateAttributes(array('id' => 'id_'.substr(md5(microtime() . $idx++), 0, 6))); } } // end func _generateId } diff --git a/lib/formslib.php b/lib/formslib.php index 5c2abd7885..8e052addd2 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -469,7 +469,9 @@ class moodleform { foreach ($elementobjs as $elementobj){ $elementclone = clone($elementobj); $name=$elementclone->getName(); - $elementclone->setName($name."[$i]"); + if (!empty($name)){ + $elementclone->setName($name."[$i]"); + } if (is_a($elementclone, 'HTML_QuickForm_header')){ $value=$elementclone->_text; $elementclone->setValue(str_replace('{no}', ($i+1), $value)); @@ -1278,7 +1280,7 @@ class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{ // switch next two lines for ol li containers for form items. // $this->_elementTemplates=array('default'=>"\n\t\t
  • error {type}\">{error}
    {element}
  • "); $this->_elementTemplates = array('default'=>"\n\t\t
    required\">{help}
    error\">{error}
    {element}
    ", - 'fieldset'=>"\n\t\t
    required\">{help}
    error\">{error}
    {element}
    "); + 'fieldset'=>"\n\t\t
    required\">
    {label}{req}{advancedimg}
    {help}
    error\">{error}
    {element}
    "); parent::HTML_QuickForm_Renderer_Tableless(); } @@ -1341,6 +1343,19 @@ class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{ } function renderElement(&$element, $required, $error){ + //manipulate id of all elements before rendering + if (!is_null($element->getAttribute('id'))) { + $id = $element->getAttribute('id'); + } else { + $id = $element->getName(); + } + //strip qf_ prefix and replace '[' with '_' and strip ']' + $id = preg_replace(array('/^qf_|\]/', '/\[/'), array('', '_'), $id); + if (strpos($id, 'id_') !== 0){ + $element->updateAttributes(array('id'=>'id_'.$id)); + } + + //adding stuff to place holders in template if (method_exists($element, 'getElementTemplateType')){ $html = $this->_elementTemplates[$element->getElementTemplateType()]; }else{ @@ -1369,22 +1384,10 @@ class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{ $html = str_replace('{help}', '', $html); } + $this->_templates[$element->getName()] = $html; - if (!is_null($element->getAttribute('id'))) { - $id = $element->getAttribute('id'); - } else { - $id = $element->getName(); - } - $id = preg_replace('/^qf_/', '', $id, 1); - if (strpos($id, 'id_') !== 0){ - $element->updateAttributes(array('id'=>'id_'.$id)); - } + parent::renderElement($element, $required, $error); - if ($element->getType() == 'static' or $element->getType() == 'date_selector' or $element->getType() == 'date_time_selector') { - //xhtml compliance - remove 'for' attribute from label if element with id does not exist - //TODO: is there a better way to do it? - $this->_html = str_replace('