How can I wrap the object generated by FromBuilder?

2

I use FormBuilder to generate an object and send it to an API, but the generated object is not quite the format that the API expects. I need to know how to add objects inside objects using FormBuild. I did a "gambiarra" to work, but wanted to know how to do it more professionally.

createForm(create) {
this.formRegister = this.formBuilder.group({
  nome: [create.nome, [ Validators.required ]],
  email: [create.email, [ Validators.required, Validators.email ]],
  celular: [create.celular, [ Validators.required ]],
  cpf: [create.cpf, [ Validators.required ]],
  senha: [create.senha, [ Validators.required ]],
  sexo: [create.sexo, [ Validators.required ]],
  dataNascimento: [create.dataNascimento, [ Validators.required ]]
});

}

I was using this function to wrap my object

objTransf() {
let data = new PreCadastro;
data = {
  'pessoa': {
    'nome': this.formRegister.value.nome,
    'email': this.formRegister.value.email,
    'cpf': this.formRegister.value.cpf,
    'sexo': this.formRegister.value.sexo,
    'dataNascimento': this.formRegister.value.dataNascimento,
    'contato': {
      'celular': this.formRegister.value.celular
    }
  },
  'usuario': {
    'login': this.formRegister.value.email,
    'senha': this.formRegister.value.senha
  },
  'preCadastro': true
  };
  this.addElement(data);
  }

If you have a link from a story or example, it would be a great help.

    
asked by anonymous 03.01.2019 / 21:10

1 answer

2

The best thing would be for every threaded object that you have. You create another formGroup inside your formGroup so it gets the structure the way you need it.

this.formRegister = this.formBuilder.group({
  pessoa: this.formBuilder.group({
      nome: [create.nome, [ Validators.required ]],
      email: [create.email, [ Validators.required, Validators.email ]],
      celular: [create.celular, [ Validators.required ]],
      cpf: [create.cpf, [ Validators.required ]],
      senha: [create.senha, [ Validators.required ]],
      sexo: [create.sexo, [ Validators.required ]],
 })
 usuario: this.formBuilder.group({
      senha: [create.senha, [ Validators.required ]],
      login: [create.email, [ Validators.required, Validators.email ]]
 })
});

ai use html in this way:

<div formGroupName="pessoa">
    
03.01.2019 / 22:01