2

I have form with implemented form array but here i can not validate basically i do not know how to get controls from form array. I have an error that controls of undefined.

Here you can see my code what i have tried

<form [formGroup]="myForm">

    <div formArrayName="addresses" >
        <div *ngFor="let address of myForm.controls.addresses.controls; let i=index" class="panel panel-default">

            <div [formGroupName]="i" class="myform">
                <label>street</label>
                <input type="text" class="form-control" formControlName="street" [class.valid]="address.street.valid"><br><br>
                <label>postcode</label>
                <input type="text" class="form-control" formControlName="postcode">
            </div><br><br>
        </div>
    </div><br><br>
        <pre>form value: <br>{{myForm.value | json}}</pre>
</form>

you can see full working example here

https://stackblitz.com/edit/angular-r4jruv?file=app%2Fapp.component.html

3 Answers 3

1

First, add a getter to your component to shorten the access path for your FormArray controls.

component.ts

fa: FormArray;

ngOnInit() {
  const fa = this._fb.array([
    this.initAddress(),
  ]);

  this.fa = fa;

  this.myForm = this._fb.group({
    addresses: fa
  });
}

Next, update the html

component.html

<div *ngFor="let address of fa.controls; let i=index" class="panel panel-default">
  <div [formGroupName]="i" class="myform">
    <label>street</label>
    <input type="text" class="form-control" formControlName="street" [class.valid]="address.valid"><br><br>
  </div>
</div>

Live demo

Sign up to request clarification or add additional context in comments.

2 Comments

Yes this is also working perfect thank you @Tomasz Kula
Glad I could help :)
0

you are accessing street directly from address but it's actually in address.controls

<form [formGroup]="myForm">

<div formArrayName="addresses" >
    <div *ngFor="let address of myForm.controls.addresses.controls; let i=index" class="panel panel-default">

        <div [formGroupName]="i" class="myform">
            <label>street</label>
            <input type="text" class="form-control" formControlName="street" [class.valid]="address.controls.street.valid"><br><br>
            <label>postcode</label>
            <input type="text" class="form-control" formControlName="postcode">
        </div><br><br>
    </div>
</div><br><br>
    <pre>form value: <br>{{myForm.value | json}}</pre>
</form>

this should work

Comments

0

You could use the form control status :

<input ... [class.valid]="address.status === 'VALID'">

Here is an edit of your stackblitz.

4 Comments

But here i can not track the status like touched and dirty. Is it possible.
You could use address.pristine and address.dirty. I edited the stackblitz.
As suggested by @Tomas Kula, using address.valid is better.
So it is not possible track touched using staus method. Anyway i am going to use @Waleed answer Thank you @ ibenjelloun for your help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.