changeset 411:d428db60fad1

refactor: Refactoring validation
author Thomas Junk <thomas.junk@intevation.de>
date Wed, 15 Aug 2018 16:54:23 +0200
parents cffd144c99ea
children 21fb992b1f5a
files client/src/components/Userdetail.vue
diffstat 1 files changed, 33 insertions(+), 37 deletions(-) [+]
line wrap: on
line diff
--- a/client/src/components/Userdetail.vue	Wed Aug 15 15:14:47 2018 +0200
+++ b/client/src/components/Userdetail.vue	Wed Aug 15 16:54:23 2018 +0200
@@ -84,7 +84,7 @@
 <script>
 import app from "../main";
 
-const clearedErrors = () => {
+const emptyErrormessages = () => {
   return {
     email: "",
     country: "",
@@ -137,9 +137,8 @@
     user() {
       this.currentUser = { ...this.user };
       this.path = this.user.name;
-      this.errors = clearedErrors();
-      this.password = "";
-      this.passwordre = "";
+      this.clearPassword();
+      this.clearErrors();
     }
   },
   computed: {
@@ -147,58 +146,55 @@
       return this.$store.getters["usermanagement/currentUser"];
     },
     isFormValid() {
-      this.validateCountry();
-      this.validateRole();
-      this.validatePassword();
-      this.validateEmailaddress();
-      const valid =
+      return (
         isEmailValid(this.currentUser.email) &&
         this.currentUser.country &&
         this.password === this.passwordre &&
-        (this.password === "" || !violatedPasswordRules(this.password));
-      return valid;
+        (this.password === "" || !violatedPasswordRules(this.password))
+      );
     }
   },
   methods: {
+    clearErrors() {
+      this.errors = emptyErrormessages();
+    },
+    clearPassword() {
+      this.password = "";
+      this.passwordre = "";
+    },
     closeDetailview() {
       this.$store.commit("usermanagement/clearCurrentUser");
       this.$store.commit("usermanagement/setUserDetailsInvisible");
     },
     validateCountry() {
-      if (this.currentUser.country) {
-        this.errors.country = "";
-      } else {
-        this.errors.country = "Please choose a country";
-      }
+      this.errors.country = this.currentUser.country
+        ? ""
+        : "Please choose a country";
     },
     validateRole() {
-      if (this.currentUser.role) {
-        this.errors.role = "";
-      } else {
-        this.errors.role = "Please choose a role";
-      }
+      this.errors.role = this.currentUser.role ? "" : "Please choose a role";
     },
     validatePassword() {
-      if (this.password !== this.passwordre) {
-        this.errors.passwordre = "Passwords do not match!";
-      } else {
-        this.errors.passwordre = "";
-      }
-      if (this.password !== "" && violatedPasswordRules(this.password)) {
-        this.errors.password =
-          "Password should at least be 8 char long including 1 digit and 1 special char like $";
-      } else {
-        this.errors.password = "";
-      }
+      this.errors.passwordre =
+        this.password === this.passwordre ? "" : "Passwords do not match!";
+      this.errors.password =
+        this.password === "" || !violatedPasswordRules(this.password)
+          ? ""
+          : "Password should at least be 8 char long including 1 digit and 1 special char like $";
     },
     validateEmailaddress() {
-      if (isEmailValid(this.currentUser.email)) {
-        this.errors.email = "";
-      } else {
-        this.errors.email = "invalid email";
-      }
+      this.errors.email = isEmailValid(this.currentUser.email)
+        ? ""
+        : "invalid email";
+    },
+    validate() {
+      this.validateCountry();
+      this.validateRole();
+      this.validatePassword();
+      this.validateEmailaddress();
     },
     save() {
+      this.validate();
       if (!this.isFormValid) return;
       if (this.password) this.currentUser.password = this.password;
       this.submitted = true;