Skip to content

Commit

Permalink
fix: checkboxes and radios have mode default/on
Browse files Browse the repository at this point in the history
  • Loading branch information
MHajoha committed Nov 20, 2024
1 parent 3cc4ced commit dc1db81
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
11 changes: 9 additions & 2 deletions classes/question_ui_renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ private function mangle_ids_and_names(): void {
/**
* Transforms input(-like) elements.
*
* - If {@see question_display_options} is set, the input is disabled.
* - If {@see question_display_options::$readonly} is set, the input is disabled.
* - If a value was saved for the input in a previous step, the latest value is added to the HTML.
*
* Requires the unmangled name of the element, so must be called _before_ {@see mangle_ids_and_names}.
Expand Down Expand Up @@ -291,7 +291,14 @@ private function set_input_values_and_readonly(): void {
$lastvalue = $this->attempt->get_last_qt_var($name);
if (!is_null($lastvalue)) {
if ($type === 'checkbox' || $type === 'radio') {
if ($element->getAttribute('value') === $lastvalue) {
// FIXME: Unchecked checkboxes send nothing, so we have no way of distinguishing an explicitly
// unchecked checkbox from a checkbox which was not submitted (e.g. because it wasn't shown).
// As it stands, a default-checked but explicitly unchecked checkbox will be checked again on next
// view.
$shouldbechecked = $element->hasAttribute('value')
? $element->getAttribute('value') === $lastvalue
: $lastvalue === 'on';
if ($shouldbechecked) {
$element->setAttribute('checked', 'checked');
} else {
$element->removeAttribute('checked');
Expand Down
6 changes: 4 additions & 2 deletions tests/question_ui_renderer_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -390,7 +390,8 @@ public function test_should_correctly_fill_data(): void {
$qa = $this->create_question_attempt_stub('deadbeef');
$newvalues = [
'my_text' => 'new',
'my_checkbox' => 'value',
'my_checkbox_value' => 'value',
'my_checkbox_on' => 'on',
'my_radio' => 'value1',
'my_select' => 'value3',
'my_hidden' => 'new',
Expand All @@ -406,7 +407,8 @@ public function test_should_correctly_fill_data(): void {
<div xmlns="http://www.w3.org/1999/xhtml" id="mangled:my_div">
<input class="form-control qpy-input" type="text" name="mangled:my_text" value="new"/>
<input class="qpy-input" type="checkbox" name="mangled:my_checkbox" value="value" checked="checked"/>
<input class="qpy-input" type="checkbox" name="mangled:my_checkbox_value" value="value" checked="checked"/>
<input class="qpy-input" type="checkbox" name="mangled:my_checkbox_on" checked="checked"/>
<input class="qpy-input" type="radio" name="mangled:my_radio" value="value1" checked="checked"/>
<input class="qpy-input" type="radio" name="mangled:my_radio" value="value2"/>
Expand Down
3 changes: 2 additions & 1 deletion tests/question_uis/input-values.xhtml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
<div xmlns="http://www.w3.org/1999/xhtml" id="my_div">
<input type="text" name="my_text" value="original"/>

<input type="checkbox" name="my_checkbox" value="value"/>
<input type="checkbox" name="my_checkbox_value" value="value"/>
<input type="checkbox" name="my_checkbox_on"/>

<input type="radio" name="my_radio" value="value1"/>
<input type="radio" name="my_radio" value="value2" checked="checked"/>
Expand Down

0 comments on commit dc1db81

Please sign in to comment.