From 523b4eaa8114fb486f2083e549883f762ac6566a Mon Sep 17 00:00:00 2001 From: Daniel Petrov Date: Sun, 26 May 2024 10:06:20 +0300 Subject: [PATCH] fix(form): allow setting null value for multiple choice input when updating form --- server/initializers/forms.js | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/server/initializers/forms.js b/server/initializers/forms.js index 5045dcde..ecab2163 100644 --- a/server/initializers/forms.js +++ b/server/initializers/forms.js @@ -379,19 +379,23 @@ function generateApiUpdate (fields) { } else { // convert [{label: {bg, en, ...}}] into {bg: [...], en: [...], ...} const mergedValues = {} - val.forEach((v) => { - // iterate over our defined languages as this is user provided structure and may contain unsafe keys - for (const lang of languages) { - if (lang in v.label) { - if (mergedValues[lang] == null) { - mergedValues[lang] = [] + if (val.length === 0) { + localField(name).update(this, null, language) + } else { + val.forEach((v) => { + // iterate over our defined languages as this is user provided structure and may contain unsafe keys + for (const lang of languages) { + if (lang in v.label) { + if (mergedValues[lang] == null) { + mergedValues[lang] = [] + } + mergedValues[lang].push(v.label[lang]) } - mergedValues[lang].push(v.label[lang]) } - } - }) - const joinedValues = mapObject(mergedValues, (values) => values.join(' | ')) - localField(name).update(this, joinedValues, language) + }) + const joinedValues = mapObject(mergedValues, (values) => values.join(' | ')) + localField(name).update(this, joinedValues, language) + } } break }