From 69e18905f5ef43177e2059dc94d4280af37e859f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Torkel=20=C3=96degaard?= Date: Thu, 27 Nov 2014 14:17:31 +0100 Subject: [PATCH] Templating: added validation to template editor for variable names, Fixes #1133 --- src/app/controllers/templateEditorCtrl.js | 31 ++++++++++++++++++----- src/app/partials/templating_editor.html | 3 +-- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/src/app/controllers/templateEditorCtrl.js b/src/app/controllers/templateEditorCtrl.js index 394946447a7..5e99208a1d1 100644 --- a/src/app/controllers/templateEditorCtrl.js +++ b/src/app/controllers/templateEditorCtrl.js @@ -33,8 +33,25 @@ function (angular, _) { }; $scope.add = function() { - $scope.variables.push($scope.current); - $scope.update(); + if ($scope.isValid()) { + $scope.variables.push($scope.current); + $scope.update(); + } + }; + + $scope.isValid = function() { + if (!$scope.current.name) { + $scope.appEvent('alert-warning', ['Validation', 'Template variable requires a name']); + return false; + } + + var sameName = _.findWhere($scope.variables, { name: $scope.current.name }); + if (sameName && sameName !== $scope.current) { + $scope.appEvent('alert-warning', ['Validation', 'Variable with the same name already exists']); + return false; + } + + return true; }; $scope.runQuery = function() { @@ -57,10 +74,12 @@ function (angular, _) { }; $scope.update = function() { - $scope.runQuery().then(function() { - $scope.reset(); - $scope.editor.index = 0; - }); + if ($scope.isValid()) { + $scope.runQuery().then(function() { + $scope.reset(); + $scope.editor.index = 0; + }); + } }; $scope.reset = function() { diff --git a/src/app/partials/templating_editor.html b/src/app/partials/templating_editor.html index 1ac2c879d1c..29734c5a4fb 100644 --- a/src/app/partials/templating_editor.html +++ b/src/app/partials/templating_editor.html @@ -47,7 +47,6 @@ -
@@ -56,7 +55,7 @@
- +