Afform - Fix data type when setting entity values
authorcolemanw <coleman@civicrm.org>
Sun, 1 Oct 2023 01:59:03 +0000 (21:59 -0400)
committercolemanw <coleman@civicrm.org>
Wed, 4 Oct 2023 00:52:21 +0000 (20:52 -0400)
ext/afform/admin/ang/afGuiEditor/afGuiFieldValue.directive.js

index 661eb4b6a32ad1a2035d57031164f59b9e689a6e..3f3d0c728252aa603b5a0c042f511865715f0a57 100644 (file)
           }
         }
 
+        function isSelect2() {
+          return $element.is('.select2-container + input');
+        }
+
         // Copied from ng-list but applied conditionally if field is multi-valued
-        var parseList = function(viewValue) {
+        var parseFieldInput = function(viewValue) {
           // If the viewValue is invalid (say required but empty) it will be `undefined`
           if (_.isUndefined(viewValue)) return;
 
-          if (!multi) {
+          if ((viewValue === '1' || viewValue === '0') && ctrl.field.data_type === 'Boolean') {
+            return viewValue === '1';
+          }
+
+          if (!multi || !isSelect2()) {
             return viewValue;
           }
 
           return list;
         };
 
+        var formatViewValue = function(value) {
+          if (Array.isArray(value)) {
+            return value.join(',');
+          }
+          if (typeof value === 'boolean') {
+            return value ? '1' : '0';
+          }
+          return value;
+        };
+
         this.$onInit = function() {
           // Copied from ng-list
-          ctrl.ngModel.$parsers.push(parseList);
-          ctrl.ngModel.$formatters.push(function(value) {
-            return _.isArray(value) ? value.join(',') : value;
-          });
+          ctrl.ngModel.$parsers.push(parseFieldInput);
+          ctrl.ngModel.$formatters.push(formatViewValue);
 
           // Copied from ng-list
           ctrl.ngModel.$isEmpty = function(value) {