Add date validation to blocked periods (#432)

This commit is contained in:
Alex Tselegidis 2023-11-17 08:10:03 +01:00
parent f3d7b78b15
commit 64ce83922d
2 changed files with 20 additions and 0 deletions

View file

@ -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.');
}
}
/**

View file

@ -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();