Humanize validation errors returned on registration

This commit is contained in:
raeno 2018-12-03 22:43:58 +04:00
parent 3fa9b39150
commit 822559afd8
3 changed files with 22 additions and 6 deletions

View file

@ -1,9 +1,10 @@
import oauthApi from '../../services/new_api/oauth.js' import oauthApi from '../../services/new_api/oauth.js'
import { humanizeErrors } from '../../modules/errors'
const registration = { const registration = {
data: () => ({ data: () => ({
user: {}, user: {},
error: false, errors: [],
registering: false registering: false
}), }),
created () { created () {
@ -37,7 +38,8 @@ const registration = {
app, app,
instance: data.instance, instance: data.instance,
username: this.user.username, username: this.user.username,
password: this.user.password}) password: this.user.password
})
.then((result) => { .then((result) => {
this.$store.commit('setToken', result.access_token) this.$store.commit('setToken', result.access_token)
this.$store.dispatch('loginUser', result.access_token) this.$store.dispatch('loginUser', result.access_token)
@ -47,10 +49,10 @@ const registration = {
} else { } else {
this.registering = false this.registering = false
response.json().then((data) => { response.json().then((data) => {
this.error = data.error this.errors = humanizeErrors(JSON.parse(data.error))
}) })
} }
} },
) )
} }
} }

View file

@ -49,8 +49,10 @@
<div class='terms-of-service' v-html="termsofservice"> <div class='terms-of-service' v-html="termsofservice">
</div> </div>
</div> </div>
<div v-if="error" class='form-group'> <div v-if="errors.length" class='form-group'>
<div class='alert error'>{{error}}</div> <div class='alert error'>
<span v-for="error in errors">{{error}}</span>
</div>
</div> </div>
</form> </form>
</div> </div>

12
src/modules/errors.js Normal file
View file

@ -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]
}, [])
}