-
-
Save wtw24/b0a92a3f17b94551aec70cfa956a5a33 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