aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorraeno <just.raeno@gmail.com>2018-12-03 22:43:58 +0400
committerraeno <just.raeno@gmail.com>2018-12-05 13:44:12 +0400
commit822559afd889262f0dc6989531a54a21a01beadc (patch)
treeb316419db2ab6fa7403398f6e013fef826e1d006
parent3fa9b39150c318972511882239304bc08f6f57ad (diff)
Humanize validation errors returned on registration
-rw-r--r--src/components/registration/registration.js10
-rw-r--r--src/components/registration/registration.vue6
-rw-r--r--src/modules/errors.js12
3 files changed, 22 insertions, 6 deletions
diff --git a/src/components/registration/registration.js b/src/components/registration/registration.js
index f7f8a720..7e8b1848 100644
--- a/src/components/registration/registration.js
+++ b/src/components/registration/registration.js
@@ -1,9 +1,10 @@
import oauthApi from '../../services/new_api/oauth.js'
+import { humanizeErrors } from '../../modules/errors'
const registration = {
data: () => ({
user: {},
- error: false,
+ errors: [],
registering: false
}),
created () {
@@ -37,7 +38,8 @@ const registration = {
app,
instance: data.instance,
username: this.user.username,
- password: this.user.password})
+ password: this.user.password
+ })
.then((result) => {
this.$store.commit('setToken', result.access_token)
this.$store.dispatch('loginUser', result.access_token)
@@ -47,10 +49,10 @@ const registration = {
} else {
this.registering = false
response.json().then((data) => {
- this.error = data.error
+ this.errors = humanizeErrors(JSON.parse(data.error))
})
}
- }
+ },
)
}
}
diff --git a/src/components/registration/registration.vue b/src/components/registration/registration.vue
index 087cab6b..867e26f0 100644
--- a/src/components/registration/registration.vue
+++ b/src/components/registration/registration.vue
@@ -49,8 +49,10 @@
<div class='terms-of-service' v-html="termsofservice">
</div>
</div>
- <div v-if="error" class='form-group'>
- <div class='alert error'>{{error}}</div>
+ <div v-if="errors.length" class='form-group'>
+ <div class='alert error'>
+ <span v-for="error in errors">{{error}}</span>
+ </div>
</div>
</form>
</div>
diff --git a/src/modules/errors.js b/src/modules/errors.js
new file mode 100644
index 00000000..25b5bd81
--- /dev/null
+++ b/src/modules/errors.js
@@ -0,0 +1,12 @@
+import {capitalize, reduce} from 'lodash'
+
+export function humanizeErrors (errors) {
+ return reduce(errors, (errs, val, k) => {
+ let message = reduce(val, (acc, message) => {
+ let key = capitalize(k.replace(/_/g, ' '))
+ return acc + [key, message].join(' ') + '. '
+ }, '')
+ return [...errs, message]
+ }, [])
+}
+