From 934c181689058ed464c437ba38292f49271ae0fe Mon Sep 17 00:00:00 2001 From: Kurund Jalmi Date: Fri, 17 Feb 2023 22:00:34 +0000 Subject: [PATCH] fix the setting defaults for radio and checkboxes when values are non-string --- ext/afform/core/Civi/Afform/FormDataModel.php | 2 +- ext/afform/core/ang/af/afField.component.js | 25 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/ext/afform/core/Civi/Afform/FormDataModel.php b/ext/afform/core/Civi/Afform/FormDataModel.php index a5e07402ea..816758c550 100644 --- a/ext/afform/core/Civi/Afform/FormDataModel.php +++ b/ext/afform/core/Civi/Afform/FormDataModel.php @@ -207,7 +207,7 @@ class FormDataModel { if ($action === 'get' && strpos($fieldName, '.')) { $namesToMatch[] = substr($fieldName, 0, strrpos($fieldName, '.')); } - $select = ['name', 'label', 'input_type', 'input_attrs', 'help_pre', 'help_post', 'options', 'fk_entity', 'required']; + $select = ['name', 'label', 'input_type', 'data_type', 'input_attrs', 'help_pre', 'help_post', 'options', 'fk_entity', 'required']; if ($action === 'get') { $select[] = 'operators'; } diff --git a/ext/afform/core/ang/af/afField.component.js b/ext/afform/core/ang/af/afField.component.js index f579eedb79..2b46d142f4 100644 --- a/ext/afform/core/ang/af/afField.component.js +++ b/ext/afform/core/ang/af/afField.component.js @@ -142,8 +142,33 @@ }); }; + // correct the type for the value, make sure numbers are numbers and not string + function correctValueType(value, dataType) { + // let's skip type correction for null values + if (value === null) { + return value; + } + + // if value is a number than change it to number + if (Array.isArray(value)) { + var newValue = []; + value.forEach((v, index) => { + newValue[index] = correctValueType(v); + }); + value = newValue; + } else if (dataType == 'Integer') { + value = +value; + } else if (dataType == 'Boolean') { + value = (value == 1); + } + return value; + } + // Set default value; ensure data type matches input type function setValue(value) { + // correct the value type + value = correctValueType(value, ctrl.defn.data_type); + if (ctrl.defn.input_type === 'Number' && ctrl.defn.search_range) { if (!_.isPlainObject(value)) { value = { -- 2.25.1