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 05e9a78
Showing 1 changed file with 9 additions and 2 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

0 comments on commit 05e9a78

Please sign in to comment.