From 44875d78989d31fde47ee1e33781c0233bbf3476 Mon Sep 17 00:00:00 2001 From: jamiesensei Date: Sat, 9 Dec 2006 14:06:15 +0000 Subject: [PATCH] added code to include hidden labels in groups in forms --- lib/form/dateselector.php | 5 +++++ lib/form/datetimeselector.php | 5 +++++ lib/form/group.php | 8 ++++++++ lib/form/select.php | 32 ++++++++++++++++++++++++++++++++ lib/form/text.php | 31 +++++++++++++++++++++++++++++++ lib/formslib.php | 8 ++++++-- mod/data/mod_form.php | 2 +- 7 files changed, 88 insertions(+), 3 deletions(-) diff --git a/lib/form/dateselector.php b/lib/form/dateselector.php index 574c0c95fb..aab42ccd41 100644 --- a/lib/form/dateselector.php +++ b/lib/form/dateselector.php @@ -83,6 +83,11 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group if($this->_options['optional']) { $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'off', null, get_string('disable'), $this->getAttributes(), true); } + foreach ($this->_elements as $element){ + if (method_exists($element, 'setHiddenLabel')){ + $element->setHiddenLabel(true); + } + } } diff --git a/lib/form/datetimeselector.php b/lib/form/datetimeselector.php index 5aa7ba108a..4f3e1968a5 100644 --- a/lib/form/datetimeselector.php +++ b/lib/form/datetimeselector.php @@ -90,6 +90,11 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group{ if($this->_options['optional']) { $this->_elements[] =& MoodleQuickForm::createElement('checkbox', 'off', null, get_string('disable'), $this->getAttributes(), true); } + foreach ($this->_elements as $element){ + if (method_exists($element, 'setHiddenLabel')){ + $element->setHiddenLabel(true); + } + } } diff --git a/lib/form/group.php b/lib/form/group.php index 88fbbb7e3e..714bdddca4 100644 --- a/lib/form/group.php +++ b/lib/form/group.php @@ -52,5 +52,13 @@ class MoodleQuickForm_group extends HTML_QuickForm_group{ function getElementTemplateType(){ return $this->_elementTemplateType; } + function setElements($elements){ + parent::setElements($elements); + foreach ($this->_elements as $element){ + if (method_exists($element, 'setHiddenLabel')){ + $element->setHiddenLabel(true); + } + } + } } ?> \ No newline at end of file diff --git a/lib/form/select.php b/lib/form/select.php index b86d295bd7..e349211b82 100644 --- a/lib/form/select.php +++ b/lib/form/select.php @@ -14,6 +14,37 @@ class MoodleQuickForm_select extends HTML_QuickForm_select{ * @var string */ var $_helpbutton=''; + var $_hiddenLabel=false; + function setHiddenLabel($hiddenLabel){ + $this->_hiddenLabel = $hiddenLabel; + } + function toHtml(){ + $this->_generateId(); + if ($this->_hiddenLabel){ + return ''.parent::toHtml(); + } else { + return parent::toHtml(); + } + } + /** + * Automatically generates and assigns an 'id' attribute for the element. + * + * Currently used to ensure that labels work on radio buttons and + * checkboxes. Per idea of Alexander Radivanovich. + * Overriden in moodleforms to remove qf_ prefix. + * + * @access private + * @return void + */ + function _generateId() + { + static $idx = 1; + + if (!$this->getAttribute('id')) { + $this->updateAttributes(array('id' => 'id_'. substr(md5(microtime() . $idx++), 0, 6))); + } + } // end func _generateId /** * set html for help button * @@ -63,5 +94,6 @@ class MoodleQuickForm_select extends HTML_QuickForm_select{ } } } // end func removeOption + } ?> \ No newline at end of file diff --git a/lib/form/text.php b/lib/form/text.php index b83ee767f7..cae359cfa8 100644 --- a/lib/form/text.php +++ b/lib/form/text.php @@ -14,6 +14,37 @@ class MoodleQuickForm_text extends HTML_QuickForm_text{ * @var string */ var $_helpbutton=''; + var $_hiddenLabel=false; + function setHiddenLabel($hiddenLabel){ + $this->_hiddenLabel = $hiddenLabel; + } + function toHtml(){ + $this->_generateId(); + if ($this->_hiddenLabel){ + return ''.parent::toHtml(); + } else { + return parent::toHtml(); + } + } + /** + * Automatically generates and assigns an 'id' attribute for the element. + * + * Currently used to ensure that labels work on radio buttons and + * checkboxes. Per idea of Alexander Radivanovich. + * Overriden in moodleforms to remove qf_ prefix. + * + * @access private + * @return void + */ + function _generateId() + { + static $idx = 1; + + if (!$this->getAttribute('id')) { + $this->updateAttributes(array('id' => 'id_'. substr(md5(microtime() . $idx++), 0, 6))); + } + } // end func _generateId /** * set html for help button * diff --git a/lib/formslib.php b/lib/formslib.php index 838838e91f..a772712414 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -89,7 +89,8 @@ class moodleform { * @return moodleform */ function moodleform($action, $customdata=null, $method='post', $target='', $attributes=null) { - $this->_formname = rtrim(get_class($this), '_form'); + //strip '_form' from the end of class name to make form 'id' attribute. + $this->_formname = preg_replace('/_form$/', '', get_class($this), 1); $this->_customdata = $customdata; $this->_form =& new MoodleQuickForm($this->_formname, $method, $action, $target, $attributes); @@ -1038,7 +1039,10 @@ class MoodleQuickForm_Renderer extends HTML_QuickForm_Renderer_Tableless{ } else { $id = $element->getName(); } - $element->updateAttributes(array('id'=>'id_'.$id)); + $id = preg_replace('/^qf_/', '', $id, 1); + if (strpos($id, 'id_') !== 0){ + $element->updateAttributes(array('id'=>'id_'.$id)); + } parent::renderElement($element, $required, $error); } function finishForm(&$form){ diff --git a/mod/data/mod_form.php b/mod/data/mod_form.php index 3e9192d833..eda4b9cde4 100644 --- a/mod/data/mod_form.php +++ b/mod/data/mod_form.php @@ -18,7 +18,7 @@ class data_mod_form extends moodleform_mod { $mform->addElement('htmleditor', 'intro', get_string('intro', 'data')); $mform->setType('intro', PARAM_RAW); - $mform->addRule('intro', get_string('required'), 'required', null, 'client'); + $mform->addRule('intro', null, 'required', null, 'client'); $mform->addElement('date_selector', 'timeavailablefrom', get_string('availablefromdate', 'data'), array('optional'=>true)); -- 2.39.5