fix: [settings] Make sure to save multi-select value as an array

pull/73/head
Sami Mokaddem 2021-10-20 12:48:13 +02:00
parent ab7db2c348
commit 6c4efc044d
No known key found for this signature in database
GPG Key ID: 164C473F627A06FA
2 changed files with 19 additions and 1 deletions

View File

@ -56,6 +56,13 @@ class SettingsTable extends AppTable
$errors[] = __('Invalid option provided');
}
}
if ($setting['type'] == 'multi-select') {
foreach ($value as $v) {
if (!in_array($v, array_keys($setting['options']))) {
$errors[] = __('Invalid option provided');
}
}
}
if (empty($errors) && !empty($setting['beforeSave'])) {
$setting['value'] = $value ?? '';
$beforeSaveResult = $this->SettingsProvider->evaluateFunctionForSetting($setting['beforeSave'], $setting);
@ -79,6 +86,11 @@ class SettingsTable extends AppTable
if ($setting['type'] == 'boolean') {
return (bool) $value;
}
if ($setting['type'] == 'multi-select') {
if (!is_array($value)) {
$value = json_decode($value);
}
}
return $value;
}

View File

@ -64,7 +64,13 @@
$settingId = str_replace('.', '_', $settingName);
$setting['value'] = $setting['value'] ?? '';
if ($setting['type'] == 'multi-select') {
$setting['value'] = json_decode(($setting['value']));
if (!is_array($setting['value'])) {
$firstChar = substr($setting['value'], 0, 1);
if ($firstChar != '{' && $firstChar != '[') { // make sure to cast a simple string into an array
$setting['value'] = sprintf('["%s"]', $setting['value']);
}
$setting['value'] = json_decode($setting['value']);
}
}
$options = [];
$options[] = $appView->Bootstrap->genNode('option', ['value' => '-1', 'data-is-empty-option' => '1'], __('Select an option'));