From a4c24cb66b3b3c70393d9adde51e5aa13834930d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bar=C4=B1=C5=9F=20Soner=20U=C5=9Fakl=C4=B1?= Date: Fri, 6 Apr 2018 11:39:15 -0400 Subject: [PATCH] fix saving multiple selects as json arrays --- public/src/modules/settings.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/public/src/modules/settings.js b/public/src/modules/settings.js index f891ea2ae4..4f278dfff1 100644 --- a/public/src/modules/settings.js +++ b/public/src/modules/settings.js @@ -457,6 +457,18 @@ define('settings', function () { return callback(err); } + // multipe selects are saved as json arrays, parse them here + $(formEl).find('select[multiple]').each(function (idx, selectEl) { + var key = $(selectEl).attr('name'); + if (key && values.hasOwnProperty(key)) { + try { + values[key] = JSON.parse(values[key]); + } catch (e) { + // Leave the value as is + } + } + }); + // Save loaded settings into ajaxify.data for use client-side ajaxify.data.settings = values; @@ -479,6 +491,7 @@ define('settings', function () { formEl = $(formEl); if (formEl.length) { var values = formEl.serializeObject(); + // "Fix" checkbox values, so that unchecked options are not omitted formEl.find('input[type="checkbox"]').each(function (idx, inputEl) { inputEl = $(inputEl); @@ -487,6 +500,12 @@ define('settings', function () { } }); + // save multiple selects as json arrays + formEl.find('select[multiple]').each(function (idx, selectEl) { + selectEl = $(selectEl); + values[selectEl.attr('name')] = JSON.stringify(selectEl.val()); + }); + socket.emit('admin.settings.set', { hash: hash, values: values,