From e1b71f6dd5df58db9825e0d6ad37849a7d35488c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timoth=C3=A9e=20Poussard?= <tpoussard@neogeo.fr>
Date: Fri, 15 Oct 2021 11:32:24 +0200
Subject: [PATCH] preven t same options and allow more than 2 options

---
 .../feature_type/FeatureTypeCustomForm.vue           | 12 +++++++++++-
 src/views/feature_type/Feature_type_edit.vue         |  8 ++++----
 2 files changed, 15 insertions(+), 5 deletions(-)

diff --git a/src/components/feature_type/FeatureTypeCustomForm.vue b/src/components/feature_type/FeatureTypeCustomForm.vue
index 5372073e..30b0a1e8 100644
--- a/src/components/feature_type/FeatureTypeCustomForm.vue
+++ b/src/components/feature_type/FeatureTypeCustomForm.vue
@@ -231,13 +231,23 @@ export default {
       },
       // * create an array, because backend expects an array
       set(newValue) {
+        this.form.options.errors = [];
         this.form.options.value = this.trimWhiteSpace(newValue).split(",");
-        this.updateStore();
+        if (this.hasDuplicates(this.form.options.value)) {
+          this.form.options.errors = [
+            "Veuillez saisir des valeurs différentes",
+          ];
+        } else {
+          this.updateStore();
+        }
       },
     },
   },
 
   methods: {
+    hasDuplicates(array) {
+      return new Set(array).size !== array.length;
+    },
     fillCustomFormData(customFormData) {
       for (let el in customFormData) {
         if (el && this.form[el] && customFormData[el]) {
diff --git a/src/views/feature_type/Feature_type_edit.vue b/src/views/feature_type/Feature_type_edit.vue
index 889b249c..6a1cefd4 100644
--- a/src/views/feature_type/Feature_type_edit.vue
+++ b/src/views/feature_type/Feature_type_edit.vue
@@ -277,10 +277,10 @@ export default {
       set(newValue) {
         console.log(newValue);
         const newColorsStyle = {
-          colors: {
-            [newValue.options[0]]: "#000000",
-            [newValue.options[1]]: "#000000",
-          },
+          colors: newValue.options.reduce((obj, key) => {
+            obj[key] = "#000000";
+            return obj;
+          }, {}),
           custom_field_name: newValue.value,
         };
         this.form.colors_style.value = newColorsStyle;
-- 
GitLab