From 64ce83922d59bc46294396e22e204af1ac8c5ed2 Mon Sep 17 00:00:00 2001 From: Alex Tselegidis Date: Fri, 17 Nov 2023 08:10:03 +0100 Subject: [PATCH] Add date validation to blocked periods (#432) --- application/models/Blocked_periods_model.php | 11 +++++++++++ assets/js/pages/blocked_periods.js | 9 +++++++++ 2 files changed, 20 insertions(+) diff --git a/application/models/Blocked_periods_model.php b/application/models/Blocked_periods_model.php index 73089aad..5b31119d 100644 --- a/application/models/Blocked_periods_model.php +++ b/application/models/Blocked_periods_model.php @@ -83,10 +83,21 @@ class Blocked_periods_model extends EA_Model { // Make sure all required fields are provided. if ( empty($blocked_period['name']) + || empty($blocked_period['start_datetime']) + || empty($blocked_period['end_datetime']) ) { throw new InvalidArgumentException('Not all required fields are provided: ' . print_r($blocked_period, TRUE)); } + + // Make sure that the start date time is before the end. + $start_date_time_object = new DateTime($blocked_period['start_datetime']); + $end_date_time_object = new DateTime($blocked_period['end_datetime']); + + if ($start_date_time_object >= $end_date_time_object) + { + throw new InvalidArgumentException('The start must be before the end date time value.'); + } } /** diff --git a/assets/js/pages/blocked_periods.js b/assets/js/pages/blocked_periods.js index 647e4741..a158d44e 100644 --- a/assets/js/pages/blocked_periods.js +++ b/assets/js/pages/blocked_periods.js @@ -261,6 +261,15 @@ App.Pages.BlockedPeriods = (function () { throw new Error(lang('fields_are_required')); } + const startDateTimeObject = App.Utils.UI.getDatetimepickerValue($startDateTime); + const endDateTimeObject = App.Utils.UI.getDatetimepickerValue($endDateTime); + + if (startDateTimeObject >= endDateTimeObject) { + $startDateTime.addClass('is-invalid'); + $endDateTime.addClass('is-invalid'); + throw new Error(lang('start_date_before_end_error')); + } + return true; } catch (error) { $blockedPeriods.find('.form-message').addClass('alert-danger').text(error.message).show();