From 1ff8423300d85100e026741111383cdabd5fce05 Mon Sep 17 00:00:00 2001 From: colemanw Date: Mon, 4 Sep 2023 16:12:44 -0400 Subject: [PATCH] Afform - Fix getting and setting boolean/numeric types Fixes dev/core#4465 Replaces https://github.com/civicrm/civicrm-core/pull/27006 --- .../ang/afGuiEditor/inputType/CheckBox.html | 2 +- ext/afform/core/ang/af/afField.component.js | 43 +++++++++++++------ ext/afform/core/ang/af/fields/CheckBox.html | 4 +- ext/afform/core/ang/af/fields/Select.html | 4 +- 4 files changed, 34 insertions(+), 19 deletions(-) diff --git a/ext/afform/admin/ang/afGuiEditor/inputType/CheckBox.html b/ext/afform/admin/ang/afGuiEditor/inputType/CheckBox.html index 2f7935f7b5..1e6762315d 100644 --- a/ext/afform/admin/ang/afGuiEditor/inputType/CheckBox.html +++ b/ext/afform/admin/ang/afGuiEditor/inputType/CheckBox.html @@ -4,4 +4,4 @@ - + diff --git a/ext/afform/core/ang/af/afField.component.js b/ext/afform/core/ang/af/afField.component.js index 38f9c66af6..c8646f718c 100644 --- a/ext/afform/core/ang/af/afField.component.js +++ b/ext/afform/core/ang/af/afField.component.js @@ -108,15 +108,15 @@ uniquePrefix = entityName + (index ? index + 1 : '') + (joinEntity ? '.' + joinEntity : '') + '.'; } // Set default value from url with uniquePrefix + fieldName - if (urlArgs && urlArgs[uniquePrefix + ctrl.fieldName]) { + if (urlArgs && ((uniquePrefix + ctrl.fieldName) in urlArgs)) { setValue(urlArgs[uniquePrefix + ctrl.fieldName]); } // Set default value from url with fieldName only - else if (urlArgs && urlArgs[ctrl.fieldName]) { + else if (urlArgs && (ctrl.fieldName in urlArgs)) { setValue(urlArgs[ctrl.fieldName]); } // Set default value based on field defn - else if (ctrl.defn.afform_default) { + else if ('afform_default' in ctrl.defn) { setValue(ctrl.defn.afform_default); } @@ -155,15 +155,22 @@ value.forEach((v, index) => { newValue[index] = correctValueType(v); }); - value = newValue; - } else if (dataType == 'Integer') { - value = +value; - } else if (dataType == 'Boolean') { - value = (value == 1); + return newValue; + } else if (dataType === 'Integer') { + return +value; + } else if (dataType === 'Boolean') { + return (value == 1); } return value; } + this.isMultiple = function() { + return ( + (['Select', 'EntityRef', 'ChainSelect'].includes(ctrl.defn.input_type) && ctrl.defn.input_attrs.multiple) || + (ctrl.defn.input_type === 'CheckBox' && ctrl.defn.options) + ); + }; + // Set default value; ensure data type matches input type function setValue(value) { // correct the value type @@ -189,10 +196,8 @@ '<=': ('' + value).split('-')[1] || '', }; } - else if (!_.isArray(value) && - ((['Select', 'EntityRef'].includes(ctrl.defn.input_type) && ctrl.defn.input_attrs.multiple) || ctrl.defn.input_type === 'CheckBox') - ) { - value = value.split(','); + else if (_.isString(value) && isMultiple()) { + value = value.split(','); } $scope.getSetValue(value); } @@ -282,7 +287,7 @@ // Getter/Setter function for fields of type select or entityRef. $scope.getSetSelect = function(val) { var currentVal = $scope.dataProvider.getFieldData()[ctrl.fieldName]; - // Setter + // Setter - transform raw string/array from Select2 into correct data type if (arguments.length) { if (ctrl.defn.is_date) { // The '{}' string is a placeholder for "choose date range" @@ -300,9 +305,15 @@ } return ($scope.dataProvider.getFieldData()[ctrl.fieldName][ctrl.search_operator] = val); } + if (ctrl.defn.data_type === 'Boolean') { + return ($scope.dataProvider.getFieldData()[ctrl.fieldName] = (val === 'true')); + } + if (ctrl.defn.data_type === 'Integer' && typeof val === 'string') { + return ($scope.dataProvider.getFieldData()[ctrl.fieldName] = val.length ? +val : null); + } return ($scope.dataProvider.getFieldData()[ctrl.fieldName] = val); } - // Getter + // Getter - transform data into a simple string or array for Select2 if (ctrl.defn.is_date) { return _.isPlainObject(currentVal) ? '{}' : currentVal; } @@ -313,6 +324,10 @@ else if (ctrl.search_operator) { return (currentVal || {})[ctrl.search_operator]; } + // Convert false to "false" and 0 to "0" + else if (!ctrl.isMultiple() && typeof currentVal !== 'string') { + return JSON.stringify(currentVal); + } return currentVal; }; diff --git a/ext/afform/core/ang/af/fields/CheckBox.html b/ext/afform/core/ang/af/fields/CheckBox.html index 14c9249ffd..8b2ac0ee02 100644 --- a/ext/afform/core/ang/af/fields/CheckBox.html +++ b/ext/afform/core/ang/af/fields/CheckBox.html @@ -1,7 +1,7 @@ -