From 95325b634c19c8bba7a6cc8736b9913492887b97 Mon Sep 17 00:00:00 2001 From: Coleman Watts Date: Thu, 9 Dec 2021 12:34:57 -0500 Subject: [PATCH] APIv4 - Fix datePicker defaults Ensures datepickers can select more than the current year (start and end date years were defaulting to 0) Fixes dev/report#84 --- Civi/Api4/Service/Spec/SpecFormatter.php | 4 ++-- js/crm.datepicker.js | 18 ++++++++++++------ 2 files changed, 14 insertions(+), 8 deletions(-) diff --git a/Civi/Api4/Service/Spec/SpecFormatter.php b/Civi/Api4/Service/Spec/SpecFormatter.php index f570542309..cee41e1ac1 100644 --- a/Civi/Api4/Service/Spec/SpecFormatter.php +++ b/Civi/Api4/Service/Spec/SpecFormatter.php @@ -275,8 +275,8 @@ class SpecFormatter { if ($inputType == 'Date' && !empty($data['custom_group_id'])) { $inputAttrs['time'] = empty($data['time_format']) ? FALSE : ($data['time_format'] == 1 ? 12 : 24); $inputAttrs['date'] = $data['date_format']; - $inputAttrs['start_date_years'] = (int) $data['start_date_years']; - $inputAttrs['end_date_years'] = (int) $data['end_date_years']; + $inputAttrs['start_date_years'] = isset($data['start_date_years']) ? (int) $data['start_date_years'] : NULL; + $inputAttrs['end_date_years'] = isset($data['end_date_years']) ? (int) $data['end_date_years'] : NULL; } if ($inputType == 'Text' && !empty($data['maxlength'])) { $inputAttrs['maxlength'] = (int) $data['maxlength']; diff --git a/js/crm.datepicker.js b/js/crm.datepicker.js index d630834c5c..ee628f118e 100644 --- a/js/crm.datepicker.js +++ b/js/crm.datepicker.js @@ -56,10 +56,10 @@ CRM.utils.copyAttributes($dataField, $dateField, ['style', 'class', 'disabled', 'aria-label']); placeholder = settings.placeholder || $dataField.attr('placeholder'); $dateField.addClass('crm-form-' + type); - if (!settings.minDate && !_.isUndefined(settings.start_date_years)) { + if (!settings.minDate && isInt(settings.start_date_years)) { settings.minDate = '' + (new Date().getFullYear() - settings.start_date_years) + '-01-01'; } - if (!settings.maxDate && !_.isUndefined(settings.end_date_years)) { + if (!settings.maxDate && isInt(settings.end_date_years)) { settings.maxDate = '' + (new Date().getFullYear() + settings.end_date_years) + '-12-31'; } if (hasDatepicker) { @@ -105,10 +105,7 @@ */ function dateHasDay() { var lowerFormat = settings.dateFormat.toLowerCase(); - if (lowerFormat.indexOf('d') < 0) { - return false; - } - return true; + return lowerFormat.indexOf('d') >= 0; } function updateInputFields(e, context) { var val = $dataField.val(), @@ -159,4 +156,13 @@ updateInputFields(); }); }; + + function isInt(value) { + if (isNaN(value)) { + return false; + } + var x = parseFloat(value); + return (x | 0) === x; + } + })(jQuery, CRM, CRM._); -- 2.25.1