MDL-71336 forms: ensure grouped date elements have unique IDs.

Previously they were each taking the ID of their parent group element.
This commit is contained in:
Paul Holden 2023-11-20 18:07:35 +00:00
parent 94ad185d09
commit 773bfa9272
No known key found for this signature in database
GPG Key ID: A81A96D6045F6164
5 changed files with 29 additions and 13 deletions

View File

@ -130,11 +130,12 @@ class MoodleQuickForm_date_selector extends MoodleQuickForm_group {
// If optional we add a checkbox which the user can use to turn if on.
if ($this->_options['optional']) {
$this->_elements[] = $this->createFormElement('checkbox', 'enabled', null,
get_string('enable'), $this->getAttributes(), true);
get_string('enable'), $this->getAttributesForFormElement(), true);
}
foreach ($dateformat as $key => $value) {
// E_STRICT creating elements without forms is nasty because it internally uses $this
$this->_elements[] = $this->createFormElement('select', $key, get_string($key, 'form'), $value, $this->getAttributes(), true);
$this->_elements[] = $this->createFormElement('select', $key, get_string($key, 'form'), $value,
$this->getAttributesForFormElement(), true);
}
// The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used.
if ($calendartype->get_name() === 'gregorian') {

View File

@ -134,26 +134,27 @@ class MoodleQuickForm_date_time_selector extends MoodleQuickForm_group {
// If optional we add a checkbox which the user can use to turn if on.
if ($this->_options['optional']) {
$this->_elements[] = $this->createFormElement('checkbox', 'enabled', null,
get_string('enable'), $this->getAttributes(), true);
get_string('enable'), $this->getAttributesForFormElement(), true);
}
$dateformat = $calendartype->get_date_order($this->_options['startyear'], $this->_options['stopyear']);
if (right_to_left()) { // Display time to the right of date, in RTL mode.
$this->_elements[] = $this->createFormElement('select', 'minute', get_string('minute', 'form'),
$minutes, $this->getAttributes(), true);
$minutes, $this->getAttributesForFormElement(), true);
$this->_elements[] = $this->createFormElement('select', 'hour', get_string('hour', 'form'),
$hours, $this->getAttributes(), true);
$hours, $this->getAttributesForFormElement(), true);
// Reverse date element (Should be: Day, Month, Year), in RTL mode.
$dateformat = array_reverse($dateformat);
}
foreach ($dateformat as $key => $date) {
// E_STRICT creating elements without forms is nasty because it internally uses $this
$this->_elements[] = $this->createFormElement('select', $key, get_string($key, 'form'), $date, $this->getAttributes(), true);
$this->_elements[] = $this->createFormElement('select', $key, get_string($key, 'form'), $date,
$this->getAttributesForFormElement(), true);
}
if (!right_to_left()) { // Display time to the left of date, in LTR mode.
$this->_elements[] = $this->createFormElement('select', 'hour', get_string('hour', 'form'), $hours,
$this->getAttributes(), true);
$this->getAttributesForFormElement(), true);
$this->_elements[] = $this->createFormElement('select', 'minute', get_string('minute', 'form'), $minutes,
$this->getAttributes(), true);
$this->getAttributesForFormElement(), true);
}
// The YUI2 calendar only supports the gregorian calendar type so only display the calendar image if this is being used.
if ($calendartype->get_name() === 'gregorian') {

View File

@ -172,10 +172,7 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group {
* Override of standard quickforms method to create this element.
*/
function _createElements() {
$attributes = $this->getAttributes();
if (is_null($attributes)) {
$attributes = [];
}
$attributes = $this->getAttributesForFormElement();
if (!isset($attributes['size'])) {
$attributes['size'] = 3;
}
@ -191,7 +188,7 @@ class MoodleQuickForm_duration extends MoodleQuickForm_group {
// If optional we add a checkbox which the user can use to turn if on
if($this->_options['optional']) {
$this->_elements[] = $this->createFormElement('checkbox', 'enabled', null,
get_string('enable'), $this->getAttributes(), true);
get_string('enable'), $attributes, true);
}
foreach ($this->_elements as $element){
if (method_exists($element, 'setHiddenLabel')){

View File

@ -155,6 +155,16 @@ class MoodleQuickForm_group extends HTML_QuickForm_group implements templatable
return call_user_func_array([$this->_mform, 'createElement'], func_get_args());
}
/**
* Return attributes suitable for passing to {@see createFormElement}, comprised of all group attributes without ID in
* order to ensure uniqueness of that value within the group
*
* @return array
*/
public function getAttributesForFormElement(): array {
return array_diff_key((array) $this->getAttributes(), array_flip(['id']));
}
public function export_for_template(renderer_base $output) {
global $OUTPUT;

7
lib/form/upgrade.txt Normal file
View File

@ -0,0 +1,7 @@
This files describes API changes in core_form libraries and APIs,
information provided here is intended especially for developers.
=== 4.1.7 ===
* The group element has a new method `getAttributesForFormElement` which should be used in conjunction
with `createFormElement` to ensure that all elements within the group have unique IDs