From 45568ecb215fc48d6e496a2690462e4f09d9b6db Mon Sep 17 00:00:00 2001 From: Victor Woeltjen Date: Tue, 2 Jun 2015 13:43:35 -0700 Subject: [PATCH] [Forms] Allow date-time controls to be optional Don't force date-time inputs to act as if they are required; allow them to be treated as optional, but enforce that this only occurs when left completely empty (as opposed to partially complete.) Supports WTD-1221. --- .../forms/res/templates/controls/datetime.html | 8 ++++---- .../forms/src/controllers/DateTimeController.js | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/platform/forms/res/templates/controls/datetime.html b/platform/forms/res/templates/controls/datetime.html index e2accf7c8f..6dae89eb8a 100644 --- a/platform/forms/res/templates/controls/datetime.html +++ b/platform/forms/res/templates/controls/datetime.html @@ -38,7 +38,7 @@ placeholder="YYYY-DDD" ng-pattern="/\d\d\d\d-\d\d\d/" ng-model='datetime.date' - ng-required='true'/> + ng-required='ngRequired || partiallyComplete'/> + ng-required='ngRequired || partiallyComplete'/> + ng-required='ngRequired || partiallyComplete'/> + ng-required='ngRequired || partiallyComplete'/> UTC diff --git a/platform/forms/src/controllers/DateTimeController.js b/platform/forms/src/controllers/DateTimeController.js index bf122c3f9c..530c966380 100644 --- a/platform/forms/src/controllers/DateTimeController.js +++ b/platform/forms/src/controllers/DateTimeController.js @@ -52,6 +52,20 @@ define( if (fullDateTime.isValid()) { $scope.ngModel[$scope.field] = fullDateTime.valueOf(); } + + // If anything is complete, say so in scope; there are + // ng-required usages that will update off of this (to + // allow datetime to be optional while still permitting + // incomplete input) + $scope.partiallyComplete = + Object.keys($scope.datetime).some(function (key) { + return $scope.datetime[key]; + }); + + // Treat empty input as an undefined value + if (!$scope.partiallyComplete) { + $scope.ngModel[$scope.field] = undefined; + } } // Update value whenever any field changes.