From 98af4c736df5f841c9d3fc88dd7a43e896ecb455 Mon Sep 17 00:00:00 2001 From: Marina Glancy Date: Wed, 10 Apr 2019 13:57:50 +0200 Subject: [PATCH] MDL-64151 form: autocomplete element remove last choice This is the same situation as was in MDL-56864 but now in 'course', 'autocomplete' and 'select' elements - if the element allows multiple selection it is impossible to remove the last element --- lib/form/autocomplete.php | 2 +- lib/form/select.php | 6 ++++-- lib/form/tags.php | 1 - lib/form/tests/autocomplete_test.php | 14 +++++++++++++- .../core_form/element-autocomplete-inline.mustache | 3 +++ .../core_form/element-autocomplete.mustache | 3 +++ .../core_form/element-select-inline.mustache | 3 +++ .../templates/core_form/element-select.mustache | 5 +++++ 8 files changed, 32 insertions(+), 5 deletions(-) diff --git a/lib/form/autocomplete.php b/lib/form/autocomplete.php index 2be3c40cde5..3d05905f86f 100644 --- a/lib/form/autocomplete.php +++ b/lib/form/autocomplete.php @@ -204,7 +204,7 @@ class MoodleQuickForm_autocomplete extends MoodleQuickForm_select { // Normally this is cleaned as a side effect of it not being a valid option, // but in this case we need to detect and skip it manually. if ($value === '_qf__force_multiselect_submission' || $value === null) { - $value = ''; + $value = $this->getMultiple() ? [] : ''; } return $this->_prepareValue($value, $assoc); } else { diff --git a/lib/form/select.php b/lib/form/select.php index 9e052dacc40..b84b37a6bd2 100644 --- a/lib/form/select.php +++ b/lib/form/select.php @@ -166,8 +166,9 @@ class MoodleQuickForm_select extends HTML_QuickForm_select implements templatabl */ function exportValue(&$submitValues, $assoc = false) { + $emptyvalue = $this->getMultiple() ? [] : ''; if (empty($this->_options)) { - return $this->_prepareValue(null, $assoc); + return $this->_prepareValue($emptyvalue, $assoc); } $value = $this->_findValue($submitValues); @@ -187,7 +188,7 @@ class MoodleQuickForm_select extends HTML_QuickForm_select implements templatabl } if (empty($cleaned)) { - return $this->_prepareValue(null, $assoc); + return $this->_prepareValue($emptyvalue, $assoc); } if ($this->getMultiple()) { return $this->_prepareValue($cleaned, $assoc); @@ -223,6 +224,7 @@ class MoodleQuickForm_select extends HTML_QuickForm_select implements templatabl $options[] = $o; } $context['options'] = $options; + $context['nameraw'] = $this->getName(); return $context; } diff --git a/lib/form/tags.php b/lib/form/tags.php index ef3719f9433..28468134ac0 100644 --- a/lib/form/tags.php +++ b/lib/form/tags.php @@ -261,7 +261,6 @@ class MoodleQuickForm_tags extends MoodleQuickForm_autocomplete { $url = new moodle_url('/tag/manage.php', array('tc' => $this->get_tag_collection())); $context['managestandardtagsurl'] = $url->out(false); } - $context['nameraw'] = $this->getName(); return $context; } diff --git a/lib/form/tests/autocomplete_test.php b/lib/form/tests/autocomplete_test.php index 17b9c61b7c7..a3b3cf88513 100644 --- a/lib/form/tests/autocomplete_test.php +++ b/lib/form/tests/autocomplete_test.php @@ -49,7 +49,7 @@ class core_form_autocomplete_testcase extends basic_testcase { $submission = array('testel' => 2); $this->assertEquals($element->exportValue($submission), 2); $submission = array('testel' => 3); - $this->assertNull($element->exportValue($submission)); + $this->assertEquals('', $element->exportValue($submission)); // A select with multiple values validates the data. $options = array('1' => 'One', 2 => 'Two'); @@ -61,6 +61,18 @@ class core_form_autocomplete_testcase extends basic_testcase { $element = new MoodleQuickForm_autocomplete('testel', null, array(), array('multiple'=>'multiple', 'ajax'=>'anything')); $submission = array('testel' => array(2, 3)); $this->assertEquals($element->exportValue($submission), array(2, 3)); + + // A select with single value without anything selected. + $options = array('1' => 'One', 2 => 'Two'); + $element = new MoodleQuickForm_autocomplete('testel', null, $options); + $submission = array(); + $this->assertEquals('', $element->exportValue($submission)); + + // A select with multiple values without anything selected. + $options = array('1' => 'One', 2 => 'Two'); + $element = new MoodleQuickForm_autocomplete('testel', null, $options, array('multiple' => 'multiple')); + $submission = array(); + $this->assertEquals([], $element->exportValue($submission)); } } diff --git a/theme/boost/templates/core_form/element-autocomplete-inline.mustache b/theme/boost/templates/core_form/element-autocomplete-inline.mustache index bc7434a19b3..175280ea15b 100644 --- a/theme/boost/templates/core_form/element-autocomplete-inline.mustache +++ b/theme/boost/templates/core_form/element-autocomplete-inline.mustache @@ -1,6 +1,9 @@ {{< core_form/element-template-inline }} {{$element}} {{^element.frozen}} + {{#element.multiple}} + + {{/element.multiple}} + {{/element.multiple}}