1

This is my ngOnInit

 ngOnInit() {
      this.formGroup = this._formBuilder.group({
        
        basic: this._formBuilder.array([this.basicGroup()]),
        experience: this._formBuilder.array([this.experienceGroup()]),
        education: this._formBuilder.array([this.educationGroup()]),
        skills: this._formBuilder.array([this.skillsGroup()]),
        contact: this._formBuilder.array([this.contactGroup()]),
        summary: this._formBuilder.array([this.summaryGroup()]),

      });

then I pass values of form group to params and print it on console.

In my console after form submission am getting the form values like this.

{
basic:[{...}]
experience:[{...}]
education:[{...}]
skills:[{...}]
contact:[{...}]
summury:[{...}]
}

I need to extract the values in basic array and the values in form group should be look like this when I console value of params


{
profile_name: value
country_id: value
and all other fields in basic
experience:[{...}]
education:[{...}]
skills:[{...}]
contact:[{...}]
summury:[{...}]
}

my basic form array looks like this

 basicGroup(): FormGroup{
      return this._formBuilder.group({
        profile_name: ['', [Validators.required]],
        country_id: ['', [Validators.required]],
        address: ['', [Validators.required]],
        years_of_experience: ['', [Validators.required]],
        cell_phone: ['', [Validators.required, Validators.pattern("^((\\+91-?)|0)?[0-9]{10}$")]],
        main_phone: ['', [Validators.pattern("^((\\+91-?)|0)?[0-9]{10}$")]],
      })
    }

How can I solve this?

Dharman
  • 30,962
  • 25
  • 85
  • 135
Ananthu A Nair
  • 328
  • 2
  • 9

1 Answers1

3

You can directly add formControls for each individual items of basicGroup instead of adding basicGroup as formArray. For example:

this.formGroup = this._formBuilder.group({
        
    profile_name: ['', [Validators.required]],
    country_id: ['', [Validators.required]],
    address: ['', [Validators.required]],
    years_of_experience: ['', [Validators.required]],
    cell_phone: ['', [Validators.required, Validators.pattern("^((\\+91-?)|0)?[0-9]{10}$")]],
    main_phone: ['', [Validators.pattern("^((\\+91-?)|0)?[0-9]{10}$")]],
    experience: this._formBuilder.array([this.experienceGroup()]),
    education: this._formBuilder.array([this.educationGroup()]),
    skills: this._formBuilder.array([this.skillsGroup()]),
    contact: this._formBuilder.array([this.contactGroup()]),
    summary: this._formBuilder.array([this.summaryGroup()]),

  });

else

Try basicGroup as a child formGroup.

basicGroup: this._formBuilder.group({
        profile_name: ['', [Validators.required]],
        country_id: ['', [Validators.required]],
        address: ['', [Validators.required]],
        years_of_experience: ['', [Validators.required]],
        cell_phone: ['', [Validators.required, Validators.pattern("^((\\+91-?)|0)?[0-9]{10}$")]],
        main_phone: ['', [Validators.pattern("^((\\+91-?)|0)?[0-9]{10}$")]]
    })

And before posting this to backend pass it though a mapper function like this:

mapper(){
   let formattedOutput = {...this.formGroup.value};
   delete formattedOutput.basicGroup;
   formattedOutput = {
      ...formattedOutput, 
      ...this.formGroup.get('basicGroup').value
   }
   return formattedOutput;
}
saran
  • 406
  • 2
  • 9
  • saran i have already done this . i am doing it in mat stepper when i do like this first step wont validate so i added basic.. when i need to post that in db it should be in this format – Ananthu A Nair Sep 30 '20 at 10:32
  • 1
    I have improved my answer for this scenario. – saran Sep 30 '20 at 10:54
  • thanks saran for your answer it works i made a small change params = Object.assign(params,this.formGroup.get('basicGroup').value)....... how can we do it back to basicGroup from parent form – Ananthu A Nair Oct 02 '20 at 04:44