From 5f137dcbe2a0debfc9ddec09e108add0ff5f93a8 Mon Sep 17 00:00:00 2001 From: Tim Hunt Date: Thu, 22 Nov 2012 15:20:38 +0000 Subject: [PATCH] MDL-36760 formslib: fix frozen elements with [] in name. This was a regression caused by MDL-30845 or, to be precise, the related issue MDL-32785. Form elements with names like multipier[0] require special handling, such as is present in HTML_QuickForm_element::_prepareValue. I have added equivalent handling to MoodleQuickForm::exportValues. I am afraid that I could not think of a way to do this reliably without duplicating code. --- lib/formslib.php | 25 ++++++++++++++++++- .../type/numerical/edit_numerical_form.php | 2 +- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/lib/formslib.php b/lib/formslib.php index e5e1830f7e1..d64b6190fb7 100644 --- a/lib/formslib.php +++ b/lib/formslib.php @@ -1631,7 +1631,7 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { $value = ''; // If we have a default value then export it. if (isset($this->_defaultValues[$varname])) { - $value = array($varname => $this->_defaultValues[$varname]); + $value = $this->prepare_fixed_value($varname, $this->_defaultValues[$varname]); } } else { $value = $this->_elements[$key]->exportValue($this->_submitValues, true); @@ -1662,6 +1662,29 @@ class MoodleQuickForm extends HTML_QuickForm_DHTMLRulesTableless { return $unfiltered; } + /** + * This is a bit of a hack, and it duplicates the code in + * HTML_QuickForm_element::_prepareValue, but I could not think of a way or + * reliably calling that code. (Think about date selectors, for example.) + * @param string $name the element name. + * @param mixed $value the fixed value to set. + * @return mixed the appropriate array to add to the $unfiltered array. + */ + protected function prepare_fixed_value($name, $value) { + if (null === $value) { + return null; + } else { + if (!strpos($name, '[')) { + return array($name => $value); + } else { + $valueAry = array(); + $myIndex = "['" . str_replace(array(']', '['), array('', "']['"), $name) . "']"; + eval("\$valueAry$myIndex = \$value;"); + return $valueAry; + } + } + } + /** * Adds a validation rule for the given field * diff --git a/question/type/numerical/edit_numerical_form.php b/question/type/numerical/edit_numerical_form.php index bd94e5b41f9..5328c9c916a 100644 --- a/question/type/numerical/edit_numerical_form.php +++ b/question/type/numerical/edit_numerical_form.php @@ -159,7 +159,7 @@ class qtype_numerical_edit_form extends question_edit_form { if ($mform->elementExists('multiplier[0]')) { $firstunit = $mform->getElement('multiplier[0]'); $firstunit->freeze(); - $firstunit->setValue('1.0'); + $mform->setDefault('multiplier[0]', '1.0'); $mform->addHelpButton('multiplier[0]', 'numericalmultiplier', 'qtype_numerical'); } }