Skip to content

Instantly share code, notes, and snippets.

@cron13
Created October 15, 2019 08:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cron13/17232ba8091ba3d98efa6db76937c608 to your computer and use it in GitHub Desktop.
Save cron13/17232ba8091ba3d98efa6db76937c608 to your computer and use it in GitHub Desktop.
parse laravel dot notation errors response
computed: {
parsedErrors() {
const parseDotNotation = function(str, val, obj) {
let currentObj = obj,
keys = str.split('.'),
i, l = Math.max(1, keys.length - 1),
key
for (i = 0; i < l; ++i) {
key = keys[i]
currentObj[key] = currentObj[key] || {}
currentObj = currentObj[key]
}
currentObj[keys[i]] = val
delete obj[str]
}
const expand = function (obj) {
for (let key in obj) {
if (obj.hasOwnProperty(key) && key.indexOf('.') !== -1) {
parseDotNotation(key, obj[key], obj)
}
}
return obj
}
return expand(this.errors)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment