4

As I have previously asked in a question

I want to create a nested form where the parent is agnostic to the kid's formControlNames

So lets say we have a component componentA.component.ts

@Component({
    selector: 'common-a',
    template: `
    <div [formGroup]="parentForm">
        <div class="form-group">
        <label>Common A[1]</label>
        <div >
            <input type="text" formControlName="valueA1">
            <small>Description 1</small>
        </div>
        <div class="form-group">
        <label>Common A[2]</label>
        <div >
            <input type="text" formControlName="valueA2">
            <small>Description 2</small>
        </div>
    </div>
    `
})


export class ComponentA implements OnInit{
    @Input() parentForm: FormGroup;


    constructor(private _fb: FormBuilder) {
    }

    ngOnInit() {

      this.parentForm.addControl("valueA1", new FormControl('', Validators.required));
      this.parentForm.addControl("valueA2", new FormControl('', Validators.required));
    }
}

And the main component.

@Component({
    selector: 'main',
    template: `
    <form [formGroup]="myForm" (ngSubmit)="onSubmit(myForm.value)">
        <div>
          <div *ngFor="let c of myForm.controls.componentA.controls; let i=index" class="form-group">
            <common-a [parentForm]="c"></common-a>
          </div>
            <div>
                <button type="submit" [disabled]="myForm.invalid">Register!</button>
                <a class="button" (click)="add_component()">Add New</a>
                <a class="button" (click)="delete_component()">Delete</a>
            </div>
        </div>
         <pre>form value: <br>{{myForm.value | json}}</pre>
    </form>
    `
})
export class MainComponent implements OnInit{
    @Input('group') public myForm: FormGroup;

    add_component() {
      const control = <FormArray>this.myForm.controls['componentA'];
      control.push(this._fb.group({}));
    }

    delete_component() {
      const control = <FormArray>this.myForm.controls['componentA'];
      control.removeAt(this.myForm.length-1);
    }

    constructor(private _fb: FormBuilder) {
    }

    ngOnInit() {
        this.myForm = this._fb.group({
          componentA : this._fb.array([this._fb.group({})])
        });
    }

    onSubmit(formValue) {
      console.log(formValue);
    }
}

My question now is how can I use patchValue to fetch data from a server endpoint and populate the values of the form array, creating new FormGroups on demand, always agnostic to the parent.

  1. One way I can see this can be done is that the parent generates new groups.

The problem with this approach, apart from the stinky architecture, is that I override the patchValue method with my own implementation and push these new groups on demand, since inside the function body I know how many there are, but the kid's OnInit will not be invoked within the function, leaving the values empty.

var self = this;
this.formArray.patchValue = (value: {[key: string]: any}, {onlySelf, emitEvent}: {onlySelf?: boolean, emitEvent?: boolean} = {}) =>{
    for(var i = 0; i < Object.keys(value).length; i++ ) {
        self.add_new();
    }
    Object.keys(value).forEach(name => {
        if (self.formArray.controls[name]) {
            self.formArray.controls[name].patchValue(value[name], {onlySelf: true, emitEvent});
        }
    });
    self.formArray.updateValueAndValidity({onlySelf, emitEvent});
}
  1. The other option would be for the kid to provide a static function to expose it's internal composition.

And again I think this is bad design.

static generate() {
    return new FormGroup({
        valueA1: new FormControl('', Validators.required),
        valueA2: new FormControl('', Validators.required)
    }); 
}

Do you have any clean solution for this kind of problem ?

Also here's a working plunkr based on my previous question

Community
  • 1
  • 1
stefanos
  • 260
  • 3
  • 13
  • If you would describe in simple words what exactly do you want to achieve. – kind user Jan 04 '17 at 13:17
  • I want to patch a json value: `{ "componentA": [ { "valueA1": "asd", "valueA2": "asd" }, { "valueA1": "asd", "valueA2": "asd" } ] }` and the MainComponent push the new `FormGroup` with the correct values. – stefanos Jan 04 '17 at 14:04
  • Did you find a solution? @stevengatsios – WarrenV Jul 08 '17 at 07:23
  • Kind of, I made custom wrappers for `FormGroup` and `FormArray`, where I accept as an Input a @Component and inject the view in an ElementRef overwriting the patchValue function. The code base is chaos right now, but if you'd like I can try to write a short answer. – stefanos Jul 10 '17 at 13:19

1 Answers1

0

Alternative solution for dynamic controls.

1: Inject ChangeDetectorRef

constructor(private _changeDetectorRef: ChangeDetectorRef) { }

2: Override patchValue

    this.control = new FormArray([]);
    let patchValue = this.control.patchValue;
    this.control.patchValue = (value: any, options?: Object) => {
      for (let i = this.control.length; i < value.length; i++) {
        // Push new item
      }

      this._changeDetectorRef.detectChanges();
      patchValue.apply(this.control, [value, options]);
    };
Ebubekir Dirican
  • 386
  • 4
  • 12