Skip to content

Instantly share code, notes, and snippets.

@wtw24
Forked from nonsocode/n-errorbag.js
Created March 9, 2019 23:23
Show Gist options
  • Save wtw24/b0a92a3f17b94551aec70cfa956a5a33 to your computer and use it in GitHub Desktop.
Save wtw24/b0a92a3f17b94551aec70cfa956a5a33 to your computer and use it in GitHub Desktop.
A simple Frontend Validation Error bag meant to work with Laravel validation
class ErrorBag {
constructor(errors = {}) {
this.setErrors(errors);
}
hasErrors() {
return !!this.keys.length;
}
get keys() {
return Object.keys(this.errors);
}
hasError(key) {
return this.keys.indexOf(key) > -1;
}
firstKey() {
return this.keys[0];
}
first(key) {
return this.errors[key] ? this.errors[key][0] : undefined;
}
setErrors(errors) {
this.errors = errors;
}
clearAll() {
this.setErrors({});
}
clear(key) {
return delete this.errors[key];
}
}
export default ErrorBag;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment