From 97f3816627dd4a66b2cddfa3498a160ac951bed1 Mon Sep 17 00:00:00 2001 From: dandds Date: Mon, 24 Sep 2018 14:50:54 -0400 Subject: [PATCH] new project Vue component handles redundant env name validation --- js/components/forms/new_project.js | 31 +++++++++++++++++----- templates/fragments/edit_project_form.html | 10 +++---- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/js/components/forms/new_project.js b/js/components/forms/new_project.js index 3b48cbf6..6201b3f5 100644 --- a/js/components/forms/new_project.js +++ b/js/components/forms/new_project.js @@ -32,7 +32,8 @@ export default { ).map(createEnvironment) return { - showError: false, + showMissingError: false, + showUniqueError: false, environments, name, } @@ -44,7 +45,11 @@ export default { updated: function() { if (this.environmentsHaveNames()) { - this.showError = false + this.showMissingError = false + } + + if (this.envNamesAreUnique()) { + this.showUniqueError = false } }, @@ -63,8 +68,13 @@ export default { return this.environments.every((e) => e.name !== "") }, + envNamesAreUnique: function () { + const names = this.environments.map((e) => e.name) + return [...new Set(names)].length == this.environments.length + }, + validateAndOpenModal: function (modalName) { - const textInputs = this.$children.reduce((previous, newVal) => { + const childrenValid = this.$children.reduce((previous, newVal) => { // display textInput error if it is not valid if (!newVal.showValid) { newVal.showError = true @@ -73,12 +83,19 @@ export default { return newVal.showValid && previous }, true) - const isValid = textInputs && this.environmentsHaveNames() + const hasNames = this.environmentsHaveNames() + const uniqNames = this.envNamesAreUnique() - if (isValid) { + if (!hasNames) { + this.showMissingError = true + } + + if (!uniqNames) { + this.showUniqueError = true + } + + if (childrenValid && hasNames && uniqNames) { this.openModal(modalName) - } else { - this.showError = true } } } diff --git a/templates/fragments/edit_project_form.html b/templates/fragments/edit_project_form.html index 3c76a73d..6cc46e0a 100644 --- a/templates/fragments/edit_project_form.html +++ b/templates/fragments/edit_project_form.html @@ -42,14 +42,12 @@ - {% if form.environment_names.errors %} - {% for error in form.environment_names.errors %} - {{ Alert(error, level="error") }} - {% endfor %} - {% endif %} -
+
{{ Alert("Provide at least one environment name.", level="error") }}
+
+ {{ Alert("Environment names must be unique.", level="error") }} +