From 0d724ec8508ac6952123c7985bf2c8ecbcb53b72 Mon Sep 17 00:00:00 2001 From: Ad Schellevis Date: Wed, 18 Oct 2023 20:27:57 +0200 Subject: [PATCH] MVC / form handling (opnsense.js) - regression in optgroup handling introduced in https://github.com/opnsense/core/commit/9206823d60f90430f26415082ba8a7b52931c294 Two issues found, when optgroup is not used and filled with an empty item, the length of the group seems to be 0. To retrieve the selected values, we should use val() as iterating the children of the select won't return the options anymore. --- src/opnsense/www/js/opnsense.js | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/src/opnsense/www/js/opnsense.js b/src/opnsense/www/js/opnsense.js index 9051ed308..508e36dee 100644 --- a/src/opnsense/www/js/opnsense.js +++ b/src/opnsense/www/js/opnsense.js @@ -77,14 +77,15 @@ function getFormData(parent) { } } // selectbox, collect selected items - var tmp_str = ""; - sourceNode.children().each(function(index){ - if ($(this).prop("selected")){ - if (tmp_str !== "") tmp_str = tmp_str + separator; - tmp_str = tmp_str + $(this).val(); - } - }); - node[keypart] = tmp_str; + if (!Array.isArray(sourceNode.val())) { + node[keypart] = sourceNode.val(); + } else { + node[keypart] = ""; + $.each(sourceNode.val(), function(idx, value){ + if (node[keypart] !== "") node[keypart] = node[keypart] + separator; + node[keypart] = node[keypart] + value; + }); + } } else if (sourceNode.prop("type") === "checkbox") { // checkbox input type if (sourceNode.prop("checked")) { @@ -178,7 +179,7 @@ function setFormData(parent,data) { }); } for (const [group, items] of Object.entries(optgroups)) { - if (group == '' && optgroups.length == 1) { + if (group == '' && optgroups.length <= 1) { targetNode.append(items); } else { targetNode.append($("").attr('label', group).append(items));