Skip to content

Commit 44ab2cc

Browse files
committed
Validation messages client side.
1 parent b3dbb61 commit 44ab2cc

File tree

3 files changed

+39
-11
lines changed

3 files changed

+39
-11
lines changed

client/src/app/categories/form.vue

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@
4040
isEditing() {
4141
return this.category.id > 0
4242
},
43+
isValid() {
44+
this.resetMessages()
45+
if (this.category.name === '') {
46+
this.setMessage({ type: 'error', message: ['Please fill category name'] })
47+
return false
48+
}
49+
return true
50+
},
4351
},
4452
4553
methods: {
@@ -72,19 +80,11 @@
7280
})
7381
}
7482
},
75-
isValid() {
76-
this.resetMessages()
77-
if (this.category.name === '') {
78-
this.setMessage({ type: 'error', message: ['Please fill category name'] })
79-
return false
80-
}
81-
return true
82-
},
8383
submit() {
8484
/**
8585
* Pre-conditions are met
8686
*/
87-
if (this.isValid()) {
87+
if (this.isValid) {
8888
/**
8989
* Shows the global spinner
9090
*/
@@ -124,7 +124,7 @@
124124
})
125125
},
126126
update() {
127-
this.$http.put(`categories/${this.category.id}/update`, { category: this.category }).then(() => {
127+
this.$http.put(`categories/${this.category.id}/update`, this.category).then(() => {
128128
/**
129129
* This event will notify the world about
130130
* the category creation. In this case

client/src/components/general/alerts.vue

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
<script>
33
import { mapState, mapActions } from 'vuex'
4+
import { isEmpty } from 'lodash'
45
56
export default {
67
computed: {
@@ -11,13 +12,18 @@
1112
hasErrorMessages() {
1213
return this.messages.error.length > 0
1314
},
15+
hasValidationMessages() {
16+
return !isEmpty(this.messages.validation)
17+
},
1418
},
1519
methods: {
1620
...mapActions(['setMessage']),
1721
dismiss(type) {
1822
let obj
1923
if (type === 'error') {
2024
obj = { type, message: [] }
25+
} else if (type === 'validation') {
26+
obj = { type, message: {} }
2127
} else {
2228
obj = { type, message: '' }
2329
}
@@ -48,5 +54,15 @@
4854
</ul>
4955
</div>
5056

57+
<!-- Validation messages -->
58+
<div class="alert alert-danger" v-show="hasValidationMessages">
59+
<button type="button" class="close" aria-label="Close" @click="dismiss('validation')">
60+
<span aria-hidden="true">&times;</span>
61+
</button>
62+
<ul>
63+
<li v-for="error in messages.validation">{{ error[0] }}</li>
64+
</ul>
65+
</div>
66+
5167
</div>
5268
</template>

client/src/plugins/http.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import axios from 'axios'
2+
import { isArray } from 'lodash'
23
import store from '../store'
34
import router from '../router'
45
import { apiUrl } from '../config'
@@ -30,7 +31,18 @@ http.interceptors.response.use(
3031
if (error.response.data.reason === 'token') {
3132
router.push({ name: 'login.index' })
3233
}
33-
store.dispatch('setMessage', { type: 'error', message: error.response.data.messages })
34+
/**
35+
* Error messages are sent in arrays
36+
*/
37+
if (isArray(error.response.data)) {
38+
store.dispatch('setMessage', { type: 'error', message: error.response.data.messages })
39+
/**
40+
* Laravel generated validation errors are
41+
* sent in an object
42+
*/
43+
} else {
44+
store.dispatch('setMessage', { type: 'validation', message: error.response.data })
45+
}
3446
store.dispatch('setFetching', { fetching: false })
3547
return Promise.reject(error)
3648
}

0 commit comments

Comments
 (0)