From 1531d44e6baf8dbfc5eedf742eacac9d9eef6bcf Mon Sep 17 00:00:00 2001 From: Urban Rotnik Date: Tue, 17 Dec 2019 17:54:45 +0100 Subject: [PATCH] Add DateTime validators --- .../renderers/columns/date_time_helper.js | 20 +-- .../repositories/validators/base_validator.js | 121 +++++++++++++++++- 2 files changed, 131 insertions(+), 10 deletions(-) diff --git a/app/assets/javascripts/repositories/renderers/columns/date_time_helper.js b/app/assets/javascripts/repositories/renderers/columns/date_time_helper.js index 7c1712ca2..ca24ce672 100644 --- a/app/assets/javascripts/repositories/renderers/columns/date_time_helper.js +++ b/app/assets/javascripts/repositories/renderers/columns/date_time_helper.js @@ -58,6 +58,7 @@ var DateTimeHelper = (function() { let endTime = endTimeContainer.find('input[type=hidden]').val(); let formId = startTimeContainer.find('input[type!=hidden]').data('form-id'); let columnId = startTimeContainer.find('input[type!=hidden]').data('column-id'); + let dataType = $container.find('.cell-range-container').data('type'); let json = {}; let hiddenField; let value; @@ -75,7 +76,8 @@ var DateTimeHelper = (function() { `; + value='${value}' + data-type="${dataType}"/>`; $container.find('.cell-range-container').html(hiddenField); startTimeContainer.find('.cell-timestamp-container').html(''); @@ -106,6 +108,7 @@ var DateTimeHelper = (function() { let hiddenFieldContainer = $input.parent().find('.cell-timestamp-container'); let hasTime = isValidTimeStr(timeStr); let hasDate = isValidDate(date); + let dataType = $container.data('type'); let value; let hiddenField; @@ -124,7 +127,8 @@ var DateTimeHelper = (function() { `; + value="${value}" + data-type="${dataType}"/>`; hiddenFieldContainer.html(hiddenField); } @@ -191,7 +195,7 @@ var DateTimeHelper = (function() { function initDateEditMode(formId, columnId, $cell, date, datetime) { let inputFields = `
-
+
${dateInputField(formId, columnId, date)}
`; @@ -205,7 +209,7 @@ var DateTimeHelper = (function() { function initTimeEditMode(formId, columnId, $cell, time, datetime) { let inputFields = `
-
+
${timeInputField(formId, columnId, time)}
`; @@ -226,7 +230,7 @@ var DateTimeHelper = (function() { function initDateTimeEditMode(formId, columnId, $cell, date, time, datetime) { let inputFields = `
-
+
${dateInputField(formId, columnId, date)} ${timeInputField(formId, columnId, time)}
@@ -249,7 +253,7 @@ var DateTimeHelper = (function() { function initDateTimeRangeEditMode(formId, columnId, $cell, startDate, startTime, startDatetime, endDate, endTime, endDatetime) { let inputFields = `
-
+
${dateInputField(formId, columnId, startDate)} @@ -289,7 +293,7 @@ var DateTimeHelper = (function() { function initDateRangeEditMode(formId, columnId, $cell, startDate, startDatetime, endDate, endDatetime) { let inputFields = `
-
+
${dateInputField(formId, columnId, startDate)} @@ -319,7 +323,7 @@ var DateTimeHelper = (function() { function initTimeRangeEditMode(formId, columnId, $cell, startTime, startDatetime, endTime, endDatetime) { let inputFields = `
-
+
${timeInputField(formId, columnId, startTime)} diff --git a/app/assets/javascripts/repositories/validators/base_validator.js b/app/assets/javascripts/repositories/validators/base_validator.js index 35f887a7f..ea342d62d 100644 --- a/app/assets/javascripts/repositories/validators/base_validator.js +++ b/app/assets/javascripts/repositories/validators/base_validator.js @@ -38,6 +38,123 @@ $.fn.dataTable.render.RepositoryNumberValueValidator = function() { return true; }; -$.fn.dataTable.render.RepositoryDateTimeValueValidator = function() { +$.fn.dataTable.render.RepositoryDateTimeValueValidator = function($input) { + let $container = $input.parents().eq(1); + let $date = $container.find('input[data-datetime-part=date]'); + let $time = $container.find('input[data-datetime-part=time]'); + + if (($date.val() === '') === ($time.val() === '')) { + return true; + } + + // will be refactored + $container.addClass('has-error'); + $container.append('Set both or none'); + return false; +}; + +$.fn.dataTable.render.RepositoryDateValueValidator = function() { return true; -} +}; + +$.fn.dataTable.render.RepositoryTimeValueValidator = function() { + return true; +}; + +$.fn.dataTable.render.RepositoryDateTimeRangeValueValidator = function($input) { + // will be refactored + let $container = $input.parents().eq(1); + let $dateS = $container.find('.start-time input[data-datetime-part=date]'); + let $timeS = $container.find('.start-time input[data-datetime-part=time]'); + let $dateE = $container.find('.end-time input[data-datetime-part=date]'); + let $timeE = $container.find('.end-time input[data-datetime-part=time]'); + let isValid = true; + let errorMessage; + let startTime; + let endTime; + let a = []; + + if ($input.val()) { + startTime = new Date(JSON.parse($input.val()).start_time); + endTime = new Date(JSON.parse($input.val()).end_time); + } + + a.push($dateS.val() === ''); + a.push($timeS.val() === ''); + a.push($dateE.val() === ''); + a.push($timeE.val() === ''); + + if (a.filter((v, i, arr) => arr.indexOf(v) === i).length > 1) { + isValid = false; + errorMessage = 'Needs to set all or none'; + } else if (($input.val()) && (startTime > endTime)) { + isValid = false; + errorMessage = 'End time is before start time'; + } + + if (isValid) { + return true; + } + + $container.addClass('has-error'); + $container.append(`${errorMessage}`); + return false; +}; + +$.fn.dataTable.render.RepositoryDateRangeValueValidator = function($input) { + // will be refactored + + let $container = $input.parents().eq(1); + let $dateS = $container.find('.start-time input[data-datetime-part=date]'); + let $dateE = $container.find('.end-time input[data-datetime-part=date]'); + let isValid = true; + let errorMessage; + let endTime; + let startTime; + if ($input.val()) { + startTime = new Date(JSON.parse($input.val()).start_time); + endTime = new Date(JSON.parse($input.val()).end_time); + } + + if (!($dateS.val() === '') === ($dateE.val() === '')) { + isValid = false; + errorMessage = 'Needs to set all or none'; + } else if (($input.val()) && (startTime > endTime)) { + isValid = false; + errorMessage = 'End date is before start date'; + } + + if (isValid) { + return true; + } + + $container.addClass('has-error'); + $container.append(`${errorMessage}`); + return false; +}; + +$.fn.dataTable.render.RepositoryTimeRangeValueValidator = function($input) { + // will be refactored + + let $container = $input.parents().eq(1); + let $timeS = $container.find('.start-time input[data-datetime-part=time]'); + let $timeE = $container.find('.end-time input[data-datetime-part=time]'); + let isValid = true; + let errorMessage; + + if (!($timeS.val() === '') === ($timeE.val() === '')) { + isValid = false; + errorMessage = 'Needs to set both or none'; + } else if ($timeS.val() > $timeE.val()) { + isValid = false; + errorMessage = 'Needs to set both or none'; + errorMessage = 'End time is before start time'; + } + if (isValid) { + return true; + } + // will be refactored + $container.addClass('has-error'); + $container.append(`${errorMessage}`); + return false; +};