Closed edukmattos closed 6 years ago
errors.add
can accept an array of errors, so you can add a bunch of errors at the same time. Does that answer your question?
Could you give me an example?
actLogin ({commit}, credentials) { return new Promise ((resolve, reject) => { commit ('mutAuthRequest'); axios.post (process.env.API_BASE_URL + '/ login', credentials) .then (response => { let token = response.data.access_token.token; let user = response.data.user; commit ('mutAuthSuccess', {token, user}); resolve (response); }) .catch (error => { if (error.response.status === 401) { this.errors = error.response.data; // ----------> How do I add multiple errors at the same time? <-------------- } reject (error); }); }); },
Assuming the errors returned contains a fieldName/errorMessage object you can do it like that:
const errors = error.response.data;
// convert an object of errors to an array.
const errorsArray = Object.keys(errors).reduce((arr, fieldName) => {
arr.push({
field: fieldName,
msg: errors[fieldName],
scope: this.pageName
});
}, []);
// add them to the bag.
this.errors.add(errorsArray);
Great ! Its works ! Thank You !
Hi ! I'm new to the world of Vue. I'm using vuex and axios (api Laravel) in my application. I'm getting server-side errors and making them available in Vue through vee-validate. My difficulty is to add all the errors coming from Laravel's api at the same time in a vuex action (store / auth.js) and make them available on a vue page (Login.vue).
mainjs
store/auth.js
pages/auth/Login.vue