Browse files

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.
  • Loading branch information...
1 parent e4592b2 commit 35314e2f91ecda13d45ae12945e4d7d67e97ef1d @timhunt timhunt committed Nov 22, 2012
Showing with 24 additions and 1 deletion.
  1. +24 −1 lib/formslib.php
View
25 lib/formslib.php
@@ -1670,7 +1670,7 @@ function exportValues($elementList = null){
$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);
@@ -1701,6 +1701,29 @@ function exportValues($elementList = null){
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
*
* If the element is in fact a group, it will be considered as a whole.

0 comments on commit 35314e2

Please sign in to comment.