Skip to content

Instantly share code, notes, and snippets.

@nonsocode
Created August 9, 2018 13:19
Show Gist options
  • Save nonsocode/e6f34a685f8be1422c425e3a20a69a4b to your computer and use it in GitHub Desktop.
Save nonsocode/e6f34a685f8be1422c425e3a20a69a4b 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