From 886fc6ef71b13738a637e82a9ab6a85cfab7a042 Mon Sep 17 00:00:00 2001 From: Lainow <107540223+Lainow@users.noreply.github.com> Date: Thu, 29 Oct 2026 16:32:19 +0100 Subject: [PATCH] Fix form submission error when a multiple dropdown field question is left empty --- CHANGELOG.md | 6 ++++ inc/questiontype.class.php | 11 +++---- tests/Units/FieldQuestionTypeTest.php | 42 +++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ad690b7c..380302d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/) and this project adheres to [Semantic Versioning](http://semver.org/). +## [UNRELEASED] + +### Fixed + +- Fix form submission error when a multiple dropdown field question is left empty + ## [1.24.7] - 2026-10-28 ### Fixed diff --git a/inc/questiontype.class.php b/inc/questiontype.class.php index 89debb67..c983c7ab 100644 --- a/inc/questiontype.class.php +++ b/inc/questiontype.class.php @@ -295,13 +295,14 @@ public function formatRawAnswer(mixed $answer, Question $question): string case 'date': return (string) $answer; case 'dropdown': - $answer = $answer['items_id']; - if (is_string($answer) || is_numeric($answer)) { - $answer = [$answer]; - } + // An empty multiple dropdown is submitted as an empty string + $ids = array_filter( + array_map(intval(...), (array) ($answer['items_id'] ?? [])), + fn(int $id) => $id > 0, + ); $itemtype = PluginFieldsDropdown::getClassname($current_field->fields['name']); - return implode(', ', array_map(fn($opt_id) => $itemtype::getById($opt_id)?->fields['name'] ?? '', $answer)); + return implode(', ', array_map(fn(int $opt_id) => $itemtype::getById($opt_id)?->fields['name'] ?? '', $ids)); case 'yesno': return $answer ? __('Yes') : __('No'); case 'datetime': diff --git a/tests/Units/FieldQuestionTypeTest.php b/tests/Units/FieldQuestionTypeTest.php index d6edf64d..845dce57 100644 --- a/tests/Units/FieldQuestionTypeTest.php +++ b/tests/Units/FieldQuestionTypeTest.php @@ -247,6 +247,48 @@ public function testFormatRawAnswerForMultipleGlpiItemDropdown(): void } } + public function testFormatRawAnswerForMultipleDropdown(): void + { + $this->login(); + + // Arrange: create a multiple plugin dropdown field + $this->fields['multiple_dropdown'] = $this->createField([ + 'label' => 'Multiple dropdown', + 'type' => 'dropdown', + 'multiple' => 1, + 'default_value' => [], + PluginFieldsContainer::getForeignKeyField() => $this->block->getID(), + 'ranking' => 2, + 'is_active' => 1, + ], ['default_value']); + + $dropdown_class = PluginFieldsDropdown::getClassname($this->fields['multiple_dropdown']->fields['name']); + $value1 = $this->createItem($dropdown_class, ['name' => 'Value 1', 'entities_id' => 0]); + $value2 = $this->createItem($dropdown_class, ['name' => 'Value 2', 'entities_id' => 0]); + + $builder = new FormBuilder("My form"); + $builder->addQuestion( + "Multiple dropdown question", + PluginFieldsQuestionType::class, + extra_data: json_encode($this->getFieldExtraDataConfig('multiple_dropdown')), + ); + $form = $this->createForm($builder); + $question = Question::getById($this->getQuestionId($form, "Multiple dropdown question")); + $question_type = new PluginFieldsQuestionType(); + + // Act & Assert: an empty selection is submitted as an empty string + $this->assertEquals('', $question_type->formatRawAnswer([ + 'itemtype' => $dropdown_class, + 'items_id' => '', + ], $question)); + + // Act & Assert: selected values are displayed + $this->assertEquals('Value 1, Value 2', $question_type->formatRawAnswer([ + 'itemtype' => $dropdown_class, + 'items_id' => [(string) $value1->getID(), (string) $value2->getID()], + ], $question)); + } + public function testFieldsQuestionEditorRenderingForMultipleDropdownWithStringKeyedDefaultValue(): void { $this->login();