Created
August 10, 2023 16:23
-
-
Save martinbean/c00b5723f4df8a62d941a6d826a210e9 to your computer and use it in GitHub Desktop.
Vue.js form object
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 { | |
public errors: Record<string, string[]>; | |
constructor(errors: Record<string, string[]> = []) { | |
this.errors = errors; | |
} | |
set(errors: Record<string, string[]>) { | |
this.errors = errors; | |
} | |
reset() { | |
this.errors = []; | |
} | |
has(key: string) { | |
return this.errors.hasOwnProperty(key) && this.errors[key].length > 0; | |
} | |
get(key: string) { | |
return this.has(key) ? this.errors[key] : []; | |
} | |
first(key: string) { | |
return this.has(key) ? this.errors[key][0] : undefined; | |
} | |
}; |
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 { | |
public data: Record<string, any>; | |
public errors: ErrorBag; | |
protected readonly initialData: Record<string, any>; | |
protected processing: boolean; | |
protected complete: boolean; | |
constructor(data: Record<string, any>) { | |
this.data = data; | |
this.initialData = { ...this.data }; | |
this.errors = new ErrorBag(); | |
this.processing = false; | |
this.complete = false; | |
} | |
reset() { | |
this.data = { ...this.initialData; }; | |
this.errors.reset(); | |
this.processing = false; | |
this.complete = false; | |
} | |
startProcessing() { | |
this.errors.reset(); | |
this.processing = true; | |
this.complete = false; | |
} | |
finishProcessing() { | |
this.processing = false; | |
this.complete = true; | |
} | |
setErrors(errors: Record<string, string[]>) { | |
this.errors.set(errors); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment