From 79f672bfa97b3cbe3bee0dd7c3f5a4385fee0075 Mon Sep 17 00:00:00 2001 From: Michael Jumper Date: Tue, 11 Aug 2015 13:09:29 -0700 Subject: [PATCH] GUAC-1213: Set blank value if date/time is completely invalid. --- .../webapp/app/form/controllers/dateFieldController.js | 9 ++++++++- .../webapp/app/form/controllers/timeFieldController.js | 9 ++++++++- .../main/webapp/app/form/directives/guacLenientDate.js | 6 +++++- .../main/webapp/app/form/directives/guacLenientTime.js | 6 +++++- 4 files changed, 26 insertions(+), 4 deletions(-) diff --git a/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js b/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js index 34acabe62..26b663f30 100644 --- a/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js +++ b/guacamole/src/main/webapp/app/form/controllers/dateFieldController.js @@ -69,7 +69,14 @@ angular.module('form').controller('dateFieldController', ['$scope', '$injector', * set. */ var parseDate = function parseDate(str) { - return new Date(str + 'T00:00Z'); + + // Parse date, return blank if invalid + var parsedDate = new Date(str + 'T00:00Z'); + if (isNaN(parsedDate.getTime())) + return null; + + return parsedDate; + }; // Update typed value when model is changed diff --git a/guacamole/src/main/webapp/app/form/controllers/timeFieldController.js b/guacamole/src/main/webapp/app/form/controllers/timeFieldController.js index 5cb831c76..58f077524 100644 --- a/guacamole/src/main/webapp/app/form/controllers/timeFieldController.js +++ b/guacamole/src/main/webapp/app/form/controllers/timeFieldController.js @@ -69,7 +69,14 @@ angular.module('form').controller('timeFieldController', ['$scope', '$injector', * set. */ var parseTime = function parseTime(str) { - return new Date('1970-01-01T' + str + 'Z'); + + // Parse time, return blank if invalid + var parsedDate = new Date('1970-01-01T' + str + 'Z'); + if (isNaN(parsedDate.getTime())) + return null; + + return parsedDate; + }; // Update typed value when model is changed diff --git a/guacamole/src/main/webapp/app/form/directives/guacLenientDate.js b/guacamole/src/main/webapp/app/form/directives/guacLenientDate.js index fe21c8efc..4bd282f79 100644 --- a/guacamole/src/main/webapp/app/form/directives/guacLenientDate.js +++ b/guacamole/src/main/webapp/app/form/directives/guacLenientDate.js @@ -63,7 +63,11 @@ angular.module('form').directive('guacLenientDate', ['$injector', var day = parseInt(match[3] || '0') || 1; // Convert to Date object - return new Date(Date.UTC(year, month - 1, day)); + var parsedDate = new Date(Date.UTC(year, month - 1, day)); + if (isNaN(parsedDate.getTime())) + return null; + + return parsedDate; }]; diff --git a/guacamole/src/main/webapp/app/form/directives/guacLenientTime.js b/guacamole/src/main/webapp/app/form/directives/guacLenientTime.js index c5a03b917..98ecd590a 100644 --- a/guacamole/src/main/webapp/app/form/directives/guacLenientTime.js +++ b/guacamole/src/main/webapp/app/form/directives/guacLenientTime.js @@ -83,7 +83,11 @@ angular.module('form').directive('guacLenientTime', ['$injector', hour %= 24; // Convert to Date object - return new Date(Date.UTC(1970, 0, 1, hour, minute, second)); + var parsedDate = new Date(Date.UTC(1970, 0, 1, hour, minute, second)); + if (isNaN(parsedDate.getTime())) + return null; + + return parsedDate; }];