title: How to validate Template-Driven-Forms on children components in angular
body: <p>I am working on Angular 10.
  Its been 3 days that I have a problem with angular template-driven and reactive forms.</p>
  <p><strong>What I want to do:</strong> Create a complex form with many child components (in order to separate lot of code). Then on one button click I want to validate all the form and the child components. I want somehow to validate children components.</p>
  <p><strong>What I created:</strong> A template-driven form with 2-way bindings because I believe its easier and not needed for my situation.</p>
  <p>I have managed to make the validation work only for the base form. I couldn't do it on the children components. I saw many posts about this issue and tried a lot but I am very confused.</p>
  <p><strong>My code :</strong></p>
  <p>mainForm.html</p>
  <pre><code>&lt;div class=&quot;card m-3&quot;&gt;
      
      &lt;div class=&quot;card-body&quot;&gt;
        &lt;fieldset&gt;
          &lt;form name=&quot;form&quot; (ngSubmit)=&quot;f.form.valid &amp;&amp; onSubmit()&quot; #f=&quot;ngForm&quot; novalidate&gt;
          
              &lt;div class=&quot;form-row&quot;&gt;
                  &lt;div class=&quot;form-group col&quot;&gt;
                      &lt;label&gt;Title&lt;/label&gt;
                      &lt;select name=&quot;title&quot; class=&quot;form-control&quot; [(ngModel)]=&quot;model.title&quot; #title=&quot;ngModel&quot; [ngClass]=&quot;{ 'is-invalid': f.submitted &amp;&amp; title.invalid }&quot; required&gt;
                          &lt;option value=&quot;&quot;&gt;&lt;/option&gt;
                          &lt;option value=&quot;Mr&quot;&gt;Mr&lt;/option&gt;
                          &lt;option value=&quot;Mrs&quot;&gt;Mrs&lt;/option&gt;
                          &lt;option value=&quot;Miss&quot;&gt;Miss&lt;/option&gt;
                          &lt;option value=&quot;Ms&quot;&gt;Ms&lt;/option&gt;
                      &lt;/select&gt;
                      &lt;div *ngIf=&quot;f.submitted &amp;&amp; title.invalid&quot; class=&quot;invalid-feedback&quot;&gt;
                          &lt;div *ngIf=&quot;title.errors.required&quot;&gt;Title is required&lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;form-group col-5&quot;&gt;
                      &lt;label&gt;First Name&lt;/label&gt;
                      &lt;input type=&quot;text&quot; name=&quot;firstName&quot; class=&quot;form-control&quot; [(ngModel)]=&quot;model.firstName&quot; #firstName=&quot;ngModel&quot; [ngClass]=&quot;{ 'is-invalid': f.submitted &amp;&amp; firstName.invalid }&quot; required&gt;
                      &lt;div *ngIf=&quot;f.submitted &amp;&amp; firstName.invalid&quot; class=&quot;invalid-feedback&quot;&gt;
                          &lt;div *ngIf=&quot;firstName.errors.required&quot;&gt;First Name is required&lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
                  &lt;div class=&quot;form-group col-5&quot;&gt;
                      &lt;label&gt;Last Name&lt;/label&gt;
                      &lt;input type=&quot;text&quot; name=&quot;lastName&quot; class=&quot;form-control&quot; [(ngModel)]=&quot;model.lastName&quot; #lastName=&quot;ngModel&quot; [ngClass]=&quot;{ 'is-invalid': f.submitted &amp;&amp; lastName.invalid }&quot; required&gt;
                      &lt;div *ngIf=&quot;f.submitted &amp;&amp; lastName.invalid&quot; class=&quot;invalid-feedback&quot;&gt;
                          &lt;div *ngIf=&quot;lastName.errors.required&quot;&gt;Last Name is required&lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;
              &lt;/div&gt;
              
              &lt;div class=&quot;form-row&quot;&gt;
                  &lt;div class=&quot;form-group col&quot;&gt;
                      &lt;label&gt;Date of Birth&lt;/label&gt;
                      &lt;input type=&quot;date&quot; name=&quot;dob&quot; class=&quot;form-control&quot; [(ngModel)]=&quot;model.dob&quot; #dob=&quot;ngModel&quot; [ngClass]=&quot;{ 'is-invalid': f.submitted &amp;&amp; dob.invalid }&quot; required pattern=&quot;^\d{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])$&quot;&gt;
                      &lt;div *ngIf=&quot;f.submitted &amp;&amp; dob.invalid&quot; class=&quot;invalid-feedback&quot;&gt;
                          &lt;div *ngIf=&quot;dob.errors.required&quot;&gt;Date of Birth is required&lt;/div&gt;
                          &lt;div *ngIf=&quot;dob.errors.pattern&quot;&gt;Date of Birth must be a valid date in the format YYYY-MM-DD&lt;/div&gt;
                      &lt;/div&gt;
                  &lt;/div&gt;             
              &lt;/div&gt;
  
  &gt;  One child component for demonstration(there are more)--code continues below
  
                &lt;app-extra-info  [(model)]=&quot;model&quot;&gt;&lt;/app-extra-info&gt; pass model in children
    
              &lt;div class=&quot;text-center&quot;&gt;
                  &lt;button class=&quot;btn btn-primary mr-1&quot;&gt;Register&lt;/button&gt;            
              &lt;/div&gt;
          &lt;/form&gt;
        &lt;/fieldset&gt;
    &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  <p>childcomponent.html:</p>
  <pre><code>  &lt;div class=&quot;form-group row&quot;&gt;
      &lt;label class=&quot;col-2 col-form-label&quot;&gt;Gender&lt;/label&gt;
      &lt;div class=&quot;col-4&quot;&gt;
        &lt;select id=&quot;gender&quot; name=&quot;gender&quot; class=&quot;form-control&quot;  required&gt;
          &lt;option&gt;Male&lt;/option&gt;
          &lt;option&gt;Female&lt;/option&gt;
           &lt;option&gt;Not Specify&lt;/option&gt;
           &lt;option&gt;&lt;/option&gt;
        &lt;/select&gt;        
      &lt;/div&gt;   
    &lt;/div&gt;
  
    &lt;div class=&quot;form-group row&quot;&gt;
      &lt;label&gt;Pet name&lt;/label&gt;
      &lt;input type=&quot;text&quot; name=&quot;petName&quot; class=&quot;form-control&quot; [(ngModel)]=&quot;model.petName&quot; #petName=&quot;ngModel&quot; [ngClass]=&quot;{ 'is-invalid': petName.invalid &amp;&amp; petName.dirty}&quot; required&gt;
      &lt;div *ngIf=&quot;petName.invalid&quot; class=&quot;invalid-feedback&quot;&gt;
          &lt;div *ngIf=&quot;petName.errors.required&quot;&gt;Pet Name is required&lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  </code></pre>
  <p>childrencomponent.ts:</p>
  <pre><code>import { Component, Input, OnInit } from '@angular/core';
  
  @Component({
    selector: 'app-extra-info',
    templateUrl: './extra-info.component.html',
    styleUrls: ['./extra-info.component.css']
  })
  export class ExtraInfoComponent implements OnInit {
  
    @Input() model: any;
    
    constructor() { }
  
    ngOnInit(): void {
    }
  
  }
  </code></pre>
  <p>parent.ts</p>
  <pre><code>import { Component, OnInit } from '@angular/core';
  
  @Component({
    selector: 'app-user-settings-form',
    templateUrl: './user-settings-form.component.html',
    styleUrls: ['./user-settings-form.component.css']
  })
  export class UserSettingsFormComponent implements OnInit {
  
    model: any = {};
  
    constructor() {
  
     }
  
    ngOnInit() {
      
    }
  
    onSubmit() {
      alert(JSON.stringify(this.model, null, 4));
    }
  </code></pre>
  <p>}</p>
  <p>The code works as it is. <strong>The problem is that when I press Register, anything in the children component wont get validated.(in this case Pet name and gender)</strong></p>
  <p>Demo picture: <a href="https://i.stack.imgur.com/VgdlC.png" rel="nofollow noreferrer">Form</a></p>
  <p><strong>Questions I have:</strong></p>
  <ol>
  <li><p>Do I need to use template-driven or reactive forms? I said before that template-driven forms are easier and not needed for my situation but, I am not quite sure about this.</p>
  </li>
  <li><p>Do I need to pass the form into the children or pass children to main form? Is there a way to implement validation to children components using Template driven form?</p>
  </li>
  </ol>
  <p>Please can someone enlighten me what is the correct thing to do, give me some feedback and any suggestion. Don't hesitate to ask me anything or if you want more details.</p>
  <p>Thank you for your time.</p>