Skip to content

Instantly share code, notes, and snippets.

@emmanuelbarturen
Created April 8, 2019 16:35
Show Gist options
  • Save emmanuelbarturen/63b88af41639fa64ea1560f84f3efaf6 to your computer and use it in GitHub Desktop.
Save emmanuelbarturen/63b88af41639fa64ea1560f84f3efaf6 to your computer and use it in GitHub Desktop.
Show error validation from laravel to vue
import Vue from "vue";
/**
* Created by emman <emmanuelbarturen@gmail.com> on 21-Aug-18.
*/
Vue.prototype.$setErrorsFromResponse = function(errorResponse) {
// only allow this function to be run if the validator exists
if(!this.hasOwnProperty('$validator')) {
return;
}
// clear errors
this.$validator.errors.clear();
// check if errors exist
if(!errorResponse.hasOwnProperty('errors')) {
return;
}
let errorFields = Object.keys(errorResponse.errors);
// insert laravel errors
errorFields.map(field => {
let errorString = errorResponse.errors[field].join(', ');
this.$validator.errors.add({
field: field,
msg: errorString
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment