Last active
June 23, 2019 18:06
-
-
Save opejovic/2d0f9e8461ae7b58a3fc24ea7dc579bf to your computer and use it in GitHub Desktop.
Vue-Forms: Errors
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 Errors { | |
/** | |
* Create a new Errors instance. | |
*/ | |
constructor() { | |
this.errors = {}; | |
} | |
/** | |
* Determine if an errors exists for the given field. | |
* | |
* @param {string} field | |
*/ | |
has(field) { | |
return this.errors.hasOwnProperty(field); | |
} | |
/** | |
* Determine if we have any errors. | |
*/ | |
any() { | |
return Object.keys(this.errors).length > 0; | |
} | |
/** | |
* Retrieve the error message for a field. | |
* | |
* @param {string} field | |
*/ | |
get(field) { | |
if (this.errors[field]) { | |
return this.errors[field][0]; | |
} | |
} | |
/** | |
* Record the new errors. | |
* | |
* @param {object} errors | |
*/ | |
record(errors) { | |
this.errors = errors; | |
} | |
/** | |
* Clear one or all error fields. | |
* | |
* @param {string|null} field | |
*/ | |
clear(field) { | |
if (field) { | |
delete this.errors[field]; | |
return; | |
} | |
this.errors = {}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment