Created
April 8, 2019 16:35
-
-
Save emmanuelbarturen/63b88af41639fa64ea1560f84f3efaf6 to your computer and use it in GitHub Desktop.
Show error validation from laravel to vue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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