Created
August 9, 2018 13:19
-
-
Save nonsocode/e6f34a685f8be1422c425e3a20a69a4b to your computer and use it in GitHub Desktop.
A simple Frontend Validation Error bag meant to work with Laravel validation
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
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