VirtualSelect element not getting proper value for nested forms

Created on 25 July 2025, 29 days ago

Problem/Motivation

The VirtualSelect form element does not work for forms with nested structures.

This is because of the vallueCallback method, that does not take into account the form structure, by not utilizing the parents , when getting the value from userInput:

  public static function valueCallback(&$element, $input, FormStateInterface $form_state): mixed {
    $fieldName = $element['#name'] . '-input';
    $form_state->addCleanValueKey($element['#name'] . '-input');
    return $form_state->getUserInput()[$fieldName] ?? [];
  }

In the code above, we do not dig into the array structure properly.

Steps to reproduce

Create a custom form where you set the virtual_select element in the nested structure:

$form = [];

$form['#tree'] = TRUE;

$form['wrapper'] = [
  '#type' => 'details',
];

$form['wrapper']['select'] = [
  '#type' => 'virtual_select',
]

Proposed resolution

Fix the valueCallback so that it uses:
- first the input parameter if not empty
- then the nested structure of the form, by using $element['#parents']

Remaining tasks

Implement

User interface changes

None

API changes

None

Data model changes

None

🐛 Bug report
Status

Active

Version

1.0

Component

Code

Created by

🇵🇱Poland trzcinski.t@gmail.com

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

Production build 0.71.5 2024