Skip to content

feat: Add multi-select dropdown support for Forms questions#1192

Open
RPGMais wants to merge 3 commits intopluginsGLPI:mainfrom
RPGMais:fix/forms-multiselect-dropdown
Open

feat: Add multi-select dropdown support for Forms questions#1192
RPGMais wants to merge 3 commits intopluginsGLPI:mainfrom
RPGMais:fix/forms-multiselect-dropdown

Conversation

@RPGMais
Copy link
Copy Markdown

@RPGMais RPGMais commented Apr 29, 2026

Summary

Fields plugin dropdowns configured with multiple=1 now work correctly when used in GLPI Forms through the PluginFieldsQuestionType ("Campo" question type).

Problem: When a Fields dropdown with multi-selection enabled was added to a Form, there was no way to enable multi-select behavior in the form editor. The PluginFieldsQuestionType lacked:

  • A toggle to enable/disable multi-selection (like the native QuestionTypeDropdown has)
  • Proper answer handling for array values in prepareEndUserAnswer()
  • Correct serialization when applying multi-select answers to tickets via PluginFieldsDestinationField
  • Multi-select condition handlers for visibility rules

Solution (3 files, 115 lines added):

  • questiontypeextradataconfig.class.php -- Added is_multiple field to the serialized config (default false, fully backward-compatible)
  • questiontype.class.php -- Added renderAdministrationOptionsTemplate() with "Allow multiple options" toggle, overrode prepareEndUserAnswer() to normalize multi-select arrays, updated renderEndUserTemplate() and renderAdministrationTemplate() to respect the flag, added MultipleChoiceFromValuesConditionHandler for multi-select fields
  • destinationfield.class.php -- Handle both dropdown and dropdown-X types with str_starts_with(), json_encode array values for multi-select fields when creating tickets

How it works

  1. In the form editor, select question type "Campo" (Field)
  2. Choose the block and dropdown field
  3. Toggle "Allow multiple options" on
  4. Save -- the end-user form renders the dropdown as multi-select (Select2 tags)
  5. Submitted answers are properly stored and applied to tickets

Design decisions

  • The toggle is independent of the field's multiple DB flag, giving form editors flexibility to enable/disable multi-select per question regardless of the field definition
  • isMultipleForQuestion() checks both the form config (extra_data.is_multiple) and the field definition (field.multiple) as fallback
  • Follows the same pattern as the core QuestionTypeDropdown implementation (same toggle markup, same condition handler class)

No breaking changes

  • is_multiple defaults to false in jsonDeserialize(), so existing questions are unaffected
  • The destinationfield.class.php change also fixes the elseif to use str_starts_with('dropdown') instead of exact match == 'dropdown', correctly handling dropdown-X types (e.g., dropdown-User)

Test plan

  • Create a Fields dropdown with multiple=1 in a block assigned to Tickets
  • Add a "Campo" question to a Form, select the dropdown field, enable "Allow multiple options"
  • Save the form -- verify extra_data contains is_multiple: 1
  • Render the form as end-user -- verify the dropdown shows as multi-select (Select2 tags)
  • Select multiple values -- verify tags appear correctly
  • Submit the form -- verify the answer is stored as a JSON array
  • Verify the created ticket has the correct multi-select values in the Fields container
  • Verify condition/visibility rules work with multi-select answers
  • Verify backward compatibility: existing "Campo" questions without is_multiple still work as single-select
Fields plugin dropdowns with multiple=1 now work correctly in GLPI Forms.
Adds "Allow multiple options" toggle to the form editor when using the
"Campo" question type, properly handles array answers in the save pipeline,
and applies multi-select values correctly when creating tickets.
@RPGMais RPGMais force-pushed the fix/forms-multiselect-dropdown branch from 1539ca7 to 5b47bc7 Compare April 29, 2026 19:12
- Include 'tab' type containers in getAvailableBlocks() so fields from
  tab containers can be used in Forms questions (not only 'dom' type)
- Fix strlen(): array given error in fields.html.twig by passing the
  decoded array value via the 'values' option instead of the value
  parameter when rendering multi-select dropdowns (both custom and
  GLPI core dropdown types)
- Add trailing comma in MultipleChoiceFromValuesConditionHandler arg
- Collapse nested if into single condition (Rector)
- Use first-class callable intval(...) instead of string 'intval' (Rector)
- Add blank line before return in getDropdownValuesForCondition (Rector)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

2 participants