Skip to content

Instantly share code, notes, and snippets.

@danLeBrown
Last active January 24, 2023 08:12
Show Gist options
  • Save danLeBrown/b54986bb2f8dc5caddaadabca818a36b to your computer and use it in GitHub Desktop.
Save danLeBrown/b54986bb2f8dc5caddaadabca818a36b to your computer and use it in GitHub Desktop.
Collect Laravel validation error and use it to display alerts in React.js, Vue.js, etc...
// this is where alert data from the server is stored
const alertDataFromServer = []
// this is the message relayed to the user
const messageToPush = []
// res.data.data (alert data) comes from the server
alertDataFromServer.push(res.data.data)
alertDataFromServer.forEach((element) => {
const errorObjects = element.errors.errors
Object.keys(errorObjects).forEach((key) => {
obj[key].forEach((e) => {
messageToPush.push({ message: e })
})
})
return true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment