Add DateTime validators

This commit is contained in:
Urban Rotnik 2019-12-17 17:54:45 +01:00
parent 2e88f194bc
commit 1531d44e6b
2 changed files with 131 additions and 10 deletions

View file

@ -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() {
<input type="hidden"
form="${formId}"
name="repository_cells[${columnId}]"
value='${value}' />`;
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() {
<input type="hidden"
form="${formId}"
name="repository_cells[${columnId}]"
value="${value}" />`;
value="${value}"
data-type="${dataType}"/>`;
hiddenFieldContainer.html(hiddenField);
}
@ -191,7 +195,7 @@ var DateTimeHelper = (function() {
function initDateEditMode(formId, columnId, $cell, date, datetime) {
let inputFields = `
<div class="form-group">
<div class="cell-timestamp-container" data-current-date="${datetime}" data-datetime-type="date"></div>
<div class="cell-timestamp-container" data-current-date="${datetime}" data-datetime-type="date" data-type="RepositoryDateValue"></div>
${dateInputField(formId, columnId, date)}
</div>
`;
@ -205,7 +209,7 @@ var DateTimeHelper = (function() {
function initTimeEditMode(formId, columnId, $cell, time, datetime) {
let inputFields = `
<div class="form-group">
<div class="cell-timestamp-container" data-current-date="${datetime}" data-datetime-type="time"></div>
<div class="cell-timestamp-container" data-current-date="${datetime}" data-datetime-type="time" data-type="RepositoryTimeValue"></div>
${timeInputField(formId, columnId, time)}
</div>
`;
@ -226,7 +230,7 @@ var DateTimeHelper = (function() {
function initDateTimeEditMode(formId, columnId, $cell, date, time, datetime) {
let inputFields = `
<div class="form-group">
<div class="cell-timestamp-container" data-current-date="${datetime}" data-datetime-type="datetime"></div>
<div class="cell-timestamp-container" data-current-date="${datetime}" data-datetime-type="datetime" data-type="RepositoryDateTimeValue"></div>
${dateInputField(formId, columnId, date)}
${timeInputField(formId, columnId, time)}
</div>
@ -249,7 +253,7 @@ var DateTimeHelper = (function() {
function initDateTimeRangeEditMode(formId, columnId, $cell, startDate, startTime, startDatetime, endDate, endTime, endDatetime) {
let inputFields = `
<div class="form-group">
<div class="cell-range-container"></div>
<div class="cell-range-container" data-type="RepositoryDateTimeRangeValue"></div>
<div class="start-time range-type">
<div class="cell-timestamp-container" data-current-date="${startDatetime}" data-datetime-type="datetime"></div>
${dateInputField(formId, columnId, startDate)}
@ -289,7 +293,7 @@ var DateTimeHelper = (function() {
function initDateRangeEditMode(formId, columnId, $cell, startDate, startDatetime, endDate, endDatetime) {
let inputFields = `
<div class="form-group">
<div class="cell-range-container"></div>
<div class="cell-range-container" data-type="RepositoryDateRangeValue"></div>
<div class="start-time range-type">
<div class="cell-timestamp-container" data-current-date="${startDatetime}" data-datetime-type="date"></div>
${dateInputField(formId, columnId, startDate)}
@ -319,7 +323,7 @@ var DateTimeHelper = (function() {
function initTimeRangeEditMode(formId, columnId, $cell, startTime, startDatetime, endTime, endDatetime) {
let inputFields = `
<div class="form-group">
<div class="cell-range-container"></div>
<div class="cell-range-container" data-type="RepositoryTimeRangeValue"></div>
<div class="start-time range-type">
<div class="cell-timestamp-container" data-current-date="${startDatetime}" data-datetime-type="time"></div>
${timeInputField(formId, columnId, startTime)}

View file

@ -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('<span class="help-block">Set both or none</span>');
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(`<span class="help-block">${errorMessage}</span>`);
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(`<span class="help-block">${errorMessage}</span>`);
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(`<span class="help-block">${errorMessage}</span>`);
return false;
};