From 91711c11270589d47d71d0de5a0bdd280bec4276 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timoth=C3=A9e=20Poussard?= <tpoussard@neogeo.fr>
Date: Tue, 12 Oct 2021 09:59:09 +0200
Subject: [PATCH] prevent sending form if duplicate & empty string in fields

---
 .../feature_type/FeatureTypeCustomForm.vue    | 45 ++++++++-----------
 src/views/feature_type/Feature_type_edit.vue  |  6 +--
 2 files changed, 21 insertions(+), 30 deletions(-)

diff --git a/src/components/feature_type/FeatureTypeCustomForm.vue b/src/components/feature_type/FeatureTypeCustomForm.vue
index 39d1349e..3e4085b3 100644
--- a/src/components/feature_type/FeatureTypeCustomForm.vue
+++ b/src/components/feature_type/FeatureTypeCustomForm.vue
@@ -284,46 +284,37 @@ export default {
     },
 
     checkUniqueName() {
-      console.log(this.$store);
-      console.log(this.$store.state);
-      console.log(this.$store.state.feature_type);
-      if (this.form.name.value) {
-        const occurences = this.$store.state.feature_type.customForms
-          .map((el) => el.name)
-          .filter((el) => el === this.form.name.value);
-        console.log("occurences", occurences);
-        console.log(occurences.length);
-        if (occurences.length > 1) {
-          console.log("duplicate", this.form.name.value);
-          this.form.name.errors = [
-            "Les champs personnalisés ne peuvent pas avoir des noms similaires.",
-          ];
-          return false;
-        }
-      }
-      this.form.name.errors = [];
-      return true;
+      const occurences = this.$store.state.feature_type.customForms
+        .map((el) => el.name)
+        .filter((el) => el === this.form.name.value);
+      return occurences.length === 1;
     },
 
     checkCustomForm() {
-      if (this.form.label.value === null) {
+      this.form.label.errors = [];
+      this.form.name.errors = [];
+      if (!this.form.label.value) {
+        //* vérifier que le label est renseigné
         this.form.label.errors = ["Veuillez compléter ce champ."];
         return false;
-      } else if (this.form.name.value === null) {
+      } else if (!this.form.name.value) {
+        //* vérifier que le nom est renseigné
         this.form.name.errors = ["Veuillez compléter ce champ."];
-        this.form.label.errors = [];
         return false;
       } else if (!this.hasRegularCharacters(this.form.name.value)) {
+        //* vérifier qu'il n'y a pas de caractères spéciaux
         this.form.name.errors = [
           "Veuillez utiliser seulement les caratères autorisés.",
         ];
-        this.form.label.errors = [];
         return false;
-      } else if (this.checkUniqueName()) {
-        this.form.label.errors = [];
-        this.form.name.errors = [];
-        return true;
+      } else if (!this.checkUniqueName()) {
+        //* vérifier si les noms sont pas dupliqués
+        this.form.name.errors = [
+          "Les champs personnalisés ne peuvent pas avoir des noms similaires.",
+        ];
+        return false;
       }
+      return true;
     },
   },
 
diff --git a/src/views/feature_type/Feature_type_edit.vue b/src/views/feature_type/Feature_type_edit.vue
index 691443a6..8786d6d2 100644
--- a/src/views/feature_type/Feature_type_edit.vue
+++ b/src/views/feature_type/Feature_type_edit.vue
@@ -153,7 +153,6 @@
           <i class="white save icon"></i>
           Créer et importer le(s) signalement(s) du geojson
         </button>
-
       </form>
     </div>
   </div>
@@ -345,13 +344,14 @@ export default {
     },
 
     checkCustomForms() {
+      let is_valid = true;
       if (this.$refs.customForms)
         for (const customForm of this.$refs.customForms) {
           if (customForm.checkCustomForm() === false) {
-            return false;
+            is_valid = false;
           }
         }
-      return true; //* fallback if all customForms returned true
+      return is_valid; //* fallback if all customForms returned true
     },
 
     checkForms() {
-- 
GitLab