Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ 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
## [UNRELEASED]

### Fixed

- Fix form submission error when a multiple dropdown field question is left empty
- Fix search criterion value on multiple dropdown fields being reset after a search, which emptied the criterion when sorting or paging results

## [1.24.7] - 2026-10-28
Expand Down
11 changes: 6 additions & 5 deletions inc/questiontype.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
42 changes: 42 additions & 0 deletions tests/Units/FieldQuestionTypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading