Skip to content

Instantly share code, notes, and snippets.

@JeanPaulll
Created March 24, 2022 23:35
Show Gist options
  • Save JeanPaulll/f2e3548f1bbe6aecc626bc4b28df4f3e to your computer and use it in GitHub Desktop.
Save JeanPaulll/f2e3548f1bbe6aecc626bc4b28df4f3e to your computer and use it in GitHub Desktop.
Formatar o formulário dinamicamente
import {EventEmitter} from '@angular/core';
import {FormArray, FormBuilder, FormGroup, Validators} from '@angular/forms';
import {UtilForm} from '../../models/util/util-form';
/**
* @author Jean Paul <jeanpaulwebb@gmail.com>
* @class FormatForm<T>
* @description Formatar o formulário dinamicamente
* @date 21/05/2020
*/
export class FormatForm<T> {
public form: FormGroup;
public noRequireds: string[] = [];
public onlyTheseRequireds: string[] = [];
public requireds: string[] = [];
public fb = new FormBuilder();
public model: T;
public onChangeRequireds: EventEmitter<any> = new EventEmitter();
public setInnerFormArrays = false;
public disableds: string[] = [];
constructor(modelDefault: T, noRequireds?: string[], disableds?: string[], setInnerFormArrays?: boolean) {
this.setModel(modelDefault, noRequireds);
if (disableds) {
this.setDisableds(disableds);
}
}
setModel(modelDefault: T, noRequireds?: string[]) {
this.model = modelDefault;
if (!noRequireds) {
noRequireds = [];
}
if (noRequireds[0] === '-') {
if (noRequireds.length > 1) {
this.requireds = noRequireds.slice(1);
}
}
this.noRequireds = noRequireds || [];
this.form = this.getForm(modelDefault);
this.mountForms(modelDefault, this.form);
}
getForm(data: T): FormGroup {
const form = this.fb.group(this.model);
// Verifica se tem objetos apenas com id e cria um FormGroup para o mesmo.
// Isso evita que os objetos que precisam apenas do id receba um objeto grande no pathValue.
Object.keys(this.model).map(c => {
if (this.model[c] && this.model[c].length == undefined) {
const obArr = Object.keys(this.model[c]);
if (obArr.length == 1 && obArr[0] === 'id') {
form.setControl(c, UtilForm.formId(this.model[c]));
}
}
});
try {
if (data) {
form.patchValue(data);
}
} catch (e) {
console.error(e.message);
}
if (this.noRequireds) {
this.setRequiredsExcept(form);
}
return form;
}
mountForms(model, form: FormGroup) {
if (!form || !model) {
return;
}
Object.keys(model).map(c => {
const mod = model[c];
if (!mod) {
return;
}
if (mod && typeof mod === 'object') {
if (Array.isArray(mod)) {
if (this.setInnerFormArrays) {
form.setControl(c, new FormArray([]));
}
} else {
form.setControl(c, this.fb.group(mod));
this.mountForms(mod, form.get(c) as FormGroup);
}
}
});
}
setRequireds(form: FormGroup, fields: string[] = []) {
Object.keys(form.controls).map(k => {
if (fields.includes(k)) {
form.controls[k].setValidators([Validators.required]);
}
});
}
setRequiredsExcept(form = this.form, excepts = this.noRequireds) {
const requireds = this.requireds || [];
if (excepts[0] === '*') {
return;
}
if (excepts[0] === '==') {
let itens = [];
Object.keys(form.controls).map(k => {
if (excepts.includes(k)) {
form.controls[k].setValidators([Validators.required]);
} else {
itens.push(k);
}
});
this.noRequireds = itens;
return;
}
this.noRequireds = excepts;
Object.keys(form.controls).map(k => {
if (!excepts.includes(k) || requireds.includes(k)) {
form.controls[k].setValidators([Validators.required]);
}
});
}
setDisableds(fields: string[] = []) {
this.disableds = fields || [];
Object.keys(this.form.controls).map(k => {
if (fields.includes(k)) {
this.form.controls[k].disable();
}
});
}
reset() {
this.form.reset();
this.form.patchValue(this.model);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment