From f4bc7e1314df3810ee170f9bab483caae32a909f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Timoth=C3=A9e=20Poussard?= <tpoussard@neogeo.fr>
Date: Wed, 29 Sep 2021 15:28:19 +0200
Subject: [PATCH] prevent empty fields

---
 src/views/project/Project_edit.vue | 50 +++++++++++++++++++++---------
 1 file changed, 35 insertions(+), 15 deletions(-)

diff --git a/src/views/project/Project_edit.vue b/src/views/project/Project_edit.vue
index 6430ea82..66787308 100644
--- a/src/views/project/Project_edit.vue
+++ b/src/views/project/Project_edit.vue
@@ -53,9 +53,13 @@
             name="thumbnail"
             id="thumbnail"
           />
-          <ul  v-if="errorThumbnail.length" id="errorlist-thumbnail" class="errorlist">
+          <ul
+            v-if="errorThumbnail.length"
+            id="errorlist-thumbnail"
+            class="errorlist"
+          >
             <li>
-               {{ errorThumbnail[0] }}
+              {{ errorThumbnail[0] }}
             </li>
           </ul>
         </div>
@@ -79,15 +83,20 @@
             <input
               type="number"
               min="0"
-              oninput="validity.valid||(value='');"
+              oninput="validity.valid||(value=0);"
               style="padding: 1px 2px"
               name="archive_feature"
               id="archive_feature"
+              @blur="checkEmpty"
               v-model="form.archive_feature"
             />
             <div class="ui label">jour(s)</div>
           </div>
-          <ul  v-if="errors_archive_feature.length" id="errorlist-achivage" class="errorlist">
+          <ul
+            v-if="errors_archive_feature.length"
+            id="errorlist-achivage"
+            class="errorlist"
+          >
             <li>
               {{ errors_archive_feature[0] }}
             </li>
@@ -99,10 +108,11 @@
             <input
               type="number"
               min="0"
-              oninput="validity.valid||(value='');"
+              oninput="validity.valid||(value=0);"
               style="padding: 1px 2px"
               name="delete_feature"
               id="delete_feature"
+              @blur="checkEmpty"
               v-model="form.delete_feature"
             />
             <div class="ui label">jour(s)</div>
@@ -212,7 +222,7 @@ export default {
         access_level_pub_feature: [],
         access_level_arch_feature: [],
       },
-      errorThumbnail:[],
+      errorThumbnail: [],
       form: {
         title: "",
         slug: "",
@@ -278,6 +288,12 @@ export default {
       reader.readAsDataURL(this.fileToImport);
     },
 
+    checkEmpty() {
+      //* forbid empty fields
+      if (!this.form.archive_feature) this.form.archive_feature = 0;
+      if (!this.form.delete_feature) this.form.delete_feature = 0;
+    },
+
     goBackNrefresh(slug) {
       let _this = this;
       // * go back to project list
@@ -314,17 +330,19 @@ export default {
           }
         })
         .catch((error) => {
-          let err_msg = "Transférez une image valide. Le fichier que vous avez transféré n'est pas une image, ou il est corrompu."
-          if (error.response.data[0])
-            err_msg = error.response.data[0]
+          let err_msg =
+            "Transférez une image valide. Le fichier que vous avez transféré n'est pas une image, ou il est corrompu.";
+          if (error.response.data[0]) err_msg = error.response.data[0];
           this.errorThumbnail.push(err_msg);
           throw error;
         });
     },
 
     checkForm() {
-      if (this.form.archive_feature > this.form.delete_feature){
-        this.errors_archive_feature.push("Le délais de suppression doit être supérieur au délais d'archivage.");
+      if (this.form.archive_feature > this.form.delete_feature) {
+        this.errors_archive_feature.push(
+          "Le délais de suppression doit être supérieur au délais d'archivage."
+        );
         return false;
       }
       for (const key in this.errors) {
@@ -375,10 +393,9 @@ export default {
             }
           })
           .catch((error) => {
-            let err_msg = "Veuillez modifier le titre de votre projet, un projet avec ce titre existe déjà."
-            if (error.response.data.title[0])
-              err_msg = error.response.data.title[0]
-            this.errors.title.push(err_msg);
+            if (error.response.data.title[0]) {
+              this.errors.title.push(error.response.data.title[0]);
+            }
             throw error;
           });
       } else if (this.action === "edit") {
@@ -398,6 +415,9 @@ export default {
             }
           })
           .catch((error) => {
+            if (error.response.data.title[0]) {
+              this.errors.title.push(error.response.data.title[0]);
+            }
             throw error;
           });
       }
-- 
GitLab