Test admin
Test admin

Reputation: 731

Form array validation not working in Angular 2

This is my form object

 this.userFormGroup = this.fb.group({
            name : ["", Validators.required],
            email : "",
            phone : ""
            address : this.fb.group({
                city : ["", Validators.required],
                state : ""
            }),
            hobbies : this.fb.array([
                this.fb.group({
                    name : ["", Validators.required]
                }),
            ])
        })

I have used this object in the below mentioned HTML.

<div formArrayName="hobbies">
                <div formGroupName="0">
                    Name <input type="text" formControlN!ame="name">  <br><br>
                    <div *ngIf="userFormGroup.get('hobbies[0].city').hasError('required')">
                        City Required
                    </div>
                </div>
            </div>

Other validations are working but form validations are not working.

Thanks in advance.

Upvotes: 1

Views: 1237

Answers (1)

vanarajcs
vanarajcs

Reputation: 978

Try this

<div *ngIf="userFormGroup.get('hobbies').controls[i].get('name').errors">
     City Required
</div>

Upvotes: 3

Related Questions