0

I have form array, where I have form control.

Here is HTML code

<div class="container">
                            <ng-container formArrayName="positions">
                                <div class="row" style="margin-top:20px;"
                                    *ngFor="let _ of positions.controls; index as i">
                                    <ng-container [formGroupName]="i">
                                        <div class="col-sm flex-3">
                                            <input class="form-control" trim-directive formControlName="name"
                                                maxlength="64" />
                                            <div class="has-danger"
                                                *ngIf="form.get('name').touched || form.get('name').dirty">
                                                <div *ngIf="form.get('name').errors?.required"
                                                    class="form-control-feedback">
                                                    {{'FieldIsRequired' | localize}}
                                                </div>
                                            </div>
                                        </div>

                                    </ng-container>
                                </div>
                            </ng-container>
                        </div>

Here is how I create it in TS file

createForm(): any {
    this.form = this._formBuilder.group({
        positions: this._formBuilder.array(
            [
                this._formBuilder.group({
                    recruitmentAgencyClientId: [this.recruitmentAgencyClientId],
                    loanAmount: ['', [Validators.required, Validators.min(2000)]],
                    name: ['', [Validators.required]]
                })
            ], Validators.required)
    });
    this.getPositionsCount();
    this.getTotalLoanAmount();
}

But when I try to run project< I get this error

TypeError: Cannot read property 'touched' of null

at this row *ngIf="form.get('name').touched || form.get('name').dirty"

How I can solve this?

1 Answer 1

2

form is outer form group. You have to do like this to access inner form group.

<div class="container">
    <ng-container formArrayName="positions">
        <div class="row" style="margin-top:20px;"
            *ngFor="let innerForm of positions.controls; index as i">
            <ng-container [formGroupName]="i">
                <div class="col-sm flex-3">
                    <input class="form-control" trim-directive formControlName="name"
                        maxlength="64" />
                    <div class="has-danger"
                        *ngIf="innerForm.get('name').touched || innerForm.get('name').dirty">
                        <div *ngIf="innerForm.get('name').errors?.required"
                            class="form-control-feedback">
                            {{'FieldIsRequired' | localize}}
                        </div>
                    </div>
                </div>

            </ng-container>
        </div>
    </ng-container>
</div>
Sign up to request clarification or add additional context in comments.

Comments

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.