From faed188149a7aec6bccc36e341fceec2287e6452 Mon Sep 17 00:00:00 2001 From: itroll42 Date: Tue, 21 Jun 2016 10:53:04 -0400 Subject: [PATCH] Min/Max validation on not required dates Updates to support allowing a min/max date, but not having the field be required. Based on ngRequired logic. If no ngRequired is specified defaults to false. --- app/scripts/input.js | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/scripts/input.js b/app/scripts/input.js index 096691b..eb52220 100644 --- a/app/scripts/input.js +++ b/app/scripts/input.js @@ -103,6 +103,12 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi maxValid = moment.isMoment(date); } + function isNotRequired(value) { + var required = $parse(attrs.ngRequired)(scope) || false; + //if a value is not required, and no value is specified pass the min check. + return (!required && !value); + } + ngModel.$formatters.push(formatter); ngModel.$parsers.unshift(parser); @@ -110,6 +116,9 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi setMin(datePickerUtils.findParam(scope, attrs.minDate)); ngModel.$validators.min = function (value) { + if (isNotRequired(value)) { + return true; + } //If we don't have a min / max value, then any value is valid. return minValid ? moment.isMoment(value) && (minDate.isSame(value) || minDate.isBefore(value)) : true; }; @@ -119,6 +128,9 @@ Module.directive('dateTime', ['$compile', '$document', '$filter', 'dateTimeConfi setMax(datePickerUtils.findParam(scope, attrs.maxDate)); ngModel.$validators.max = function (value) { + if (isNotRequired(value)) { + return true; + } return maxValid ? moment.isMoment(value) && (maxDate.isSame(value) || maxDate.isAfter(value)) : true; }; }