Reputation: 2667
How can I eliminate the appends in the FormData. Is there a way to get a FormGroup as FormData easily? The code is so repetitive and it seems somehow unnecessary.
formularioApartamentoLocacao: FormGroup;
const formulario = new FormData();
formulario.append('preco', this.formularioApartamentoLocacao.controls['preco'].value ) ;
formulario.append('banheiros', this.formularioApartamentoLocacao.controls['banheiros'].value);
formulario.append('vagas_garagem', this.formularioApartamentoLocacao.controls['vagas_garagem'].value);
formulario.append('suites', this.formularioApartamentoLocacao.controls['su'].value);
formulario.append('foto_1', this.formularioApartamentoLocacao.controls['foto_1'].value);
formulario.append('foto_2', this.formularioApartamentoLocacao.controls['foto_2'].value);
formulario.append('foto_3', this.formularioApartamentoLocacao.controls['foto_3'].value);
formulario.append('foto_4', this.formularioApartamentoLocacao.controls['foto_4'].value);
formulario.append('foto_5', this.formularioApartamentoLocacao.controls['foto_5'].value);
formulario.append('foto_6', this.formularioApartamentoLocacao.controls['foto_6'].value);
formulario.append('foto_7', this.formularioApartamentoLocacao.controls['foto_7'].value);
formulario.append('dormitorios', this.formularioApartamentoLocacao.controls['dormitorios'].value);
Upvotes: 1
Views: 1438
Reputation: 214
Are you open to using a loop..?
This should eliminate the repetitive code:
formularioApartamentoLocacao: FormGroup;
const formulario = new FormData();
const formData = formularioApartamentoLocacao.value;
Object.keys(formData).forEach((key) => {
formulario.append(key, formData[key]);
});
Upvotes: 1