From f48da0c0d0cb8a0f612a67c53a6fa5ca8592f5e6 Mon Sep 17 00:00:00 2001 From: Thibeau Fuhrer Date: Mon, 25 Nov 2024 11:22:30 +0100 Subject: [PATCH] [FIX] UI: properly count inputs of Field\HasDynamicInputs templates. (#8422) --- .../js/Input/Field/dynamic_inputs_renderer.js | 21 ++++++++++++++----- .../Component/Input/Field/Renderer.php | 19 ++++++++++++++++- 2 files changed, 34 insertions(+), 6 deletions(-) diff --git a/components/ILIAS/UI/resources/js/Input/Field/dynamic_inputs_renderer.js b/components/ILIAS/UI/resources/js/Input/Field/dynamic_inputs_renderer.js index 168c2ec68cd0..6a5fa18bb310 100755 --- a/components/ILIAS/UI/resources/js/Input/Field/dynamic_inputs_renderer.js +++ b/components/ILIAS/UI/resources/js/Input/Field/dynamic_inputs_renderer.js @@ -1,4 +1,19 @@ /** + * This file is part of ILIAS, a powerful learning management system + * published by ILIAS open source e-Learning e.V. + * + * ILIAS is licensed with the GPL-3.0, + * see https://www.gnu.org/licenses/gpl-3.0.en.html + * You should have received a copy of said license along with the + * source code, too. + * + * If this is not the case or you just want to try ILIAS, you'll find + * us at: + * https://www.ilias.de + * https://github.com/ILIAS-eLearning + * + * ******************************************************************* + * * this script is responsible for clientside rendering of Inputs * ILIAS\UI\Component\Input\Field\DynamicInputsAware. * @@ -82,13 +97,9 @@ il.UI.Input = il.UI.Input || {}; * @param {int} sub_input_count */ let addInputTemplateIds = function (template_html, sub_input_count) { - if (1 >= sub_input_count) { - return replaceAll(template_html, INPUT_ID_PLACEHOLDER, generateId()); - } - // Ids must not be all the same, therefore we need to generate // one for each sub-input contained in the template. - for (let i = 0; i < sub_input_count; i++) { + for (let i = 0; i <= sub_input_count; i++) { template_html = replaceAll( template_html, `${INPUT_ID_PLACEHOLDER}_${i}`, diff --git a/components/ILIAS/UI/src/Implementation/Component/Input/Field/Renderer.php b/components/ILIAS/UI/src/Implementation/Component/Input/Field/Renderer.php index ee1bce23c599..8a3c0266d2fd 100755 --- a/components/ILIAS/UI/src/Implementation/Component/Input/Field/Renderer.php +++ b/components/ILIAS/UI/src/Implementation/Component/Input/Field/Renderer.php @@ -960,7 +960,7 @@ protected function initClientsideRenderer( string $template_html ): FI\HasDynamicInputs { $dynamic_inputs_template_html = $this->replaceTemplateIds($template_html); - $dynamic_input_count = count($input->getDynamicInputs()); + $dynamic_input_count = $this->countInputsWithoutGroupsRecursively($input->getTemplateForDynamicInputs()); // note that $dynamic_inputs_template_html is in tilted single quotes (`), // because otherwise the html syntax might collide with normal ones. @@ -980,6 +980,23 @@ protected function initClientsideRenderer( }); } + /** + * Counts all inputs and nested inputs of groups, without counting groups themselves. + */ + protected function countInputsWithoutGroupsRecursively(FormInput|Input $input): int + { + if (!($input instanceof Component\Input\Group)) { + return 1; + } + + $count = 0; + foreach ($input->getInputs() as $sub_input) { + $count += $this->countInputsWithoutGroupsRecursively($sub_input); + } + + return $count; + } + protected function replaceTemplateIds(string $template_html): string { // regex matches anything between 'id="' and '"', hence the js_id.