5

I have 2 components, a parent component and a child component. The parent component contains the following:

   <form (ngSubmit)="saveWebsite();" #adminForm="ngForm">

         <input type="text" name="WebName" [(ngModel)]="milestone.WebName" class="form-control" required/>

          <app-documents [documents]="caseData.CaseWebsiteDocuments" [caseId]="caseId" (fileEvent)="showToast($event)"
          (documentsEvent)="documentsEvent($event)"></app-documents>

      <button type="submit" class="btn btn-success pull-right" *ngIf="caseId">Save</button>
    </form>

The child component contains the following:

<input  type="text" [(ngModel)]="doc.FriendlyName" name="friendlyName" class="form-control" required/>

If I put all inputs in the parent component, the validation works for everything. I am trying to check the dirty status. Right now, if I make changes on the Parent, the dirty status is set to true, but if I make a change on the child, the dirty status does not change. How can I get validation to work in template driven nested controls?

2 Answers 2

14

You can provide ControlContainer on your child component like

import { ControlContainer, NgForm } from '@angular/forms';

@Component({
  selector: 'app-documents'
  ...,
  viewProviders: [ { provide: ControlContainer, useExisting: NgForm } ]
})
export class AppDocumentsComponent {}

See also

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

6 Comments

Wow, nice. I did the following and just added ngModelGroup="documents" to a container div in my child component and it works. Thanks a bunch!!!
How can I implement it in that case if I have more nested parts (forms) below the main form?
@Laszlo Can you provide an example on stackblitz?
I cannot provide an example, just think about it, you have the main form and it contains more Tab sheets, each tab sheet contains an individual component which has a lot of data entry fields. The submit button on the main form depends on all mandatory fields on various tab sheets.
@Laszlo If you have more nested forms then you probably want to use ngModelGroup. It also will work like in my solution but with only one difference: you need to use common version from the link in the question. Here's an example ng-run.com/edit/YLSx6OJVKYUbXQTsWOx2
|
1

That alone didn't work for me I added ngModel in my input too. Without ngModel I think you can't validate your forms... whether it is a child or own component forms.

import { ControlContainer, NgForm } from '@angular/forms';

@Component({
  selector: 'app-documents'
  ...,
  viewProviders: [ { provide: ControlContainer, useExisting: NgForm } ]
})
export class AppDocumentsComponent {}

that works for me!!

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.