Created
November 27, 2022 01:56
-
-
Save dacastro4/00d48f86584b13e7f21e7098bf27854d to your computer and use it in GitHub Desktop.
Laravel Vue Error Handling Class
This file contains hidden or 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
export default class FormErrors { | |
errors = [] | |
constructor(errors = []) { | |
this.errors = errors | |
} | |
/** | |
* Returns the first message from key | |
* | |
* @param {string} key | |
* @param {string | null} def | |
*/ | |
first(key, def = null) { | |
if (this.errors.hasOwnProperty(key)) { | |
return this.errors[key][0] | |
} | |
return def | |
} | |
/** | |
* Returns the all messages from key | |
* | |
* @param {string} key | |
* | |
* @return {string | null} | |
*/ | |
get(key) { | |
if (this.errors.hasOwnProperty(key)) { | |
return this.errors[key] | |
} | |
return null | |
} | |
/** | |
* Checks if error exists | |
* | |
* @param {string} key | |
* | |
* @return {boolean} | |
*/ | |
has(key) { | |
return this.errors.hasOwnProperty(key) | |
} | |
/** | |
* @param {Array} errors | |
*/ | |
record(errors) { | |
this.errors = errors | |
} | |
/** | |
* Clears errors | |
*/ | |
clear() { | |
this.errors = [] | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage
Using in Vue component