From 37bfa3c3a80f316fa6f6608fc16d36c64228a13e Mon Sep 17 00:00:00 2001 From: Michael Abashian Date: Thu, 23 Mar 2017 11:24:52 -0400 Subject: [PATCH] Add validation to the multi-select questions in survey --- awx/ui/client/src/shared/directives.js | 29 +++++++++++++++++++ .../render/multiple-choice.partial.html | 2 +- 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/awx/ui/client/src/shared/directives.js b/awx/ui/client/src/shared/directives.js index 0255aaee8b..3531e78f0b 100644 --- a/awx/ui/client/src/shared/directives.js +++ b/awx/ui/client/src/shared/directives.js @@ -1371,4 +1371,33 @@ function(ConfigurationUtils, i18n, $rootScope) { }); } }; +}]) + +.directive('awRequireMultiple', [function() { + return { + require: 'ngModel', + link: function postLink(scope, element, attrs, ngModel) { + // Watch for changes to the required attribute + attrs.$observe('required', function(value) { + if(value) { + ngModel.$validators.required = function (value) { + if(angular.isArray(value)) { + if(value.length === 0) { + return false; + } + else { + return (!value[0] || value[0] === "") ? false : true; + } + } + else { + return false; + } + }; + } + else { + delete ngModel.$validators.required; + } + }); + } + }; }]); diff --git a/awx/ui/client/src/templates/survey-maker/render/multiple-choice.partial.html b/awx/ui/client/src/templates/survey-maker/render/multiple-choice.partial.html index 580c8e5805..7fa7e1fa0d 100644 --- a/awx/ui/client/src/templates/survey-maker/render/multiple-choice.partial.html +++ b/awx/ui/client/src/templates/survey-maker/render/multiple-choice.partial.html @@ -1,5 +1,5 @@
-