Skip to content

Instantly share code, notes, and snippets.

@martinbean
Created August 10, 2023 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save martinbean/c00b5723f4df8a62d941a6d826a210e9 to your computer and use it in GitHub Desktop.
Save martinbean/c00b5723f4df8a62d941a6d826a210e9 to your computer and use it in GitHub Desktop.
Vue.js form object
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;
}
};
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