From 4cbc306b68219f0932e3922d80477c8b24fc750a Mon Sep 17 00:00:00 2001 From: Michael Abashian Date: Wed, 28 Jun 2017 11:49:15 -0400 Subject: [PATCH] Added awRange directive which watches min/max values for changes and updates validation accordingly. --- awx/ui/client/src/shared/directives.js | 35 +++++++++++++++++++ .../shared/question-definition.form.js | 10 +++--- 2 files changed, 41 insertions(+), 4 deletions(-) diff --git a/awx/ui/client/src/shared/directives.js b/awx/ui/client/src/shared/directives.js index b06d6bfa3d..e817c0d2c4 100644 --- a/awx/ui/client/src/shared/directives.js +++ b/awx/ui/client/src/shared/directives.js @@ -349,6 +349,41 @@ function(ConfigurationUtils, i18n, $rootScope) { }; }]) +.directive('awRange', ['Empty', function(Empty) { + return { + restrict: 'A', + require: 'ngModel', + link: function(scope, elem, attr, ctrl) { + + let checkRange = function(viewValue){ + ctrl.$setValidity('awRangeMin', true); + ctrl.$setValidity('awRangeMax', true); + var max = (attr.rangeMax) ? scope.$eval(attr.rangeMax) : Infinity; + var min = (attr.rangeMin) ? scope.$eval(attr.rangeMin) : -Infinity; + if (!Empty(max) && !Empty(viewValue) && Number(viewValue) > max) { + ctrl.$setValidity('awRangeMax', false); + } + else if(!Empty(min) && !Empty(viewValue) && Number(viewValue) < min) { + ctrl.$setValidity('awRangeMin', false); + } + return viewValue; + }; + + scope.$watch(attr.rangeMin, function () { + checkRange(scope.$eval(attr.ngModel)); + }); + + scope.$watch(attr.rangeMax, function () { + checkRange(scope.$eval(attr.ngModel)); + }); + + ctrl.$parsers.unshift(function(viewValue) { + return checkRange(viewValue); + }); + } + }; +}]) + .directive('smartFloat', function() { var FLOAT_REGEXP = /^\-?\d+((\.|\,)\d+)?$/; return { diff --git a/awx/ui/client/src/templates/survey-maker/shared/question-definition.form.js b/awx/ui/client/src/templates/survey-maker/shared/question-definition.form.js index 65a1ce862c..f57a5fe019 100644 --- a/awx/ui/client/src/templates/survey-maker/shared/question-definition.form.js +++ b/awx/ui/client/src/templates/survey-maker/shared/question-definition.form.js @@ -232,9 +232,10 @@ export default type: 'custom', control: '
'+ ''+ - ''+ + ''+ '
Please enter a valid integer.
'+ - '
Please enter a value in the range of {{int_min}} to {{int_max}}.
'+ + '
Please enter a minimum default of {{int_min}}.
'+ + '
Please enter a maximum default of {{int_max}}.
'+ '
', column: 2, ngShow: 'type.type === "integer" ', @@ -245,9 +246,10 @@ export default type: 'custom', control: '
'+ ''+ - ''+ + ''+ '
Please enter a valid float.
'+ - '
Please enter a value in the range of {{float_min}} to {{float_max}}!
'+ + '
Please enter a minimum default of {{float_min}}.
'+ + '
Please enter a maximum default of {{float_max}}.
'+ '
', column: 2, ngShow: 'type.type=== "float" ',