title: Angular 2 template driven form group validation
body: <p>I have a question regarding the angular 2 template driven form. I have set up one of this form and I would like to be able to give the user a warning display if one input in a form group is invalid.</p>
  
  <p>For example, let's say I have the following form :</p>
  
  <pre><code>&lt;form class="form" #f="ngForm" (submit)="submit()"&gt;
      &lt;div class="form-group"&gt;
         &lt;input type="text" name="firstName" required [(ngModel)]="user.firstName"&gt;
         &lt;input type="text" name="lastName" required [(ngModel)]="user.lastName"&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
          &lt;input type="text name="address" required [(ngModel)]="user.address"&gt;
      &lt;/div&gt;
      &lt;button type="submit" [disabled]="!f.valid"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><br/>
  I would like the entire form-group that contains the input "firstName" and input "lastName" to change if either the firstName and/or the lastName is invalid. I know I could do something like that :</p>
  
  <pre><code>&lt;div class="form-group" [class.has-error]="!firstName.valid || !lastName.valid"&gt;
     &lt;input type="text" name="firstName" required [(ngModel)]="user.firstName" #firstName="ngModel"&gt;
     &lt;input type="text" name="lastName" required [(ngModel)]="user.lastName" #lastName="ngModel"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><br/>
  It will works just fine. But here is the tricky part : in this example I only have two inputs with a simple validation rule, so it's easy to check and still readable. But what if I have like 10 inputs to check in a form-group ? I don't want to end up having to manually check the validity of every inputs.</p>
  
  <p>One of the solution I found is to create a subform inside the first one :</p>
  
  <pre><code>&lt;form class="form" #f="ngForm" (submit)="submit()"&gt;
      &lt;form #subForm="ngForm" [class.has-error]="!subForm.valid"&gt;
         &lt;input type="text" name="firstName" required [(ngModel)]="user.firstName"&gt;
         &lt;input type="text" name="lastName" required [(ngModel)]="user.lastName"&gt;
      &lt;/form&gt;
      &lt;div class="form-group"&gt;
          &lt;input type="text name="address" required [(ngModel)]="user.address"&gt;
      &lt;/div&gt;
      &lt;button type="submit" [disabled]="!f.valid || subForm.valid"&gt;Submit&lt;/button&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p><br/>
  Here is a plunker I created to illustrate : 
  <a href="https://embed.plnkr.co/4u8SW8GOcyYXEFRpW6AO/" rel="nofollow noreferrer">form validation example</a></p>
  
  <p>But I find it quite ugly, and I'm force to check the two forms to know if there is any problems. So finally my question is : can I set a div as a angular 2 ngForm to be able to validate multiple inputs at once ? Basically is there a better way to perform this kind of validation than creating a subform ? Something like that for example : </p>
  
  <pre><code>&lt;div class="form-group" #names [class.has-error]="!names.valid"&gt;
     &lt;input type="text" name="firstName" required [(ngModel)]="user.firstName" #firstName="ngModel"&gt;
     &lt;input type="text" name="lastName" required [(ngModel)]="user.lastName" #lastName="ngModel"&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p><br/></p>
  
  <p>PS : I know using a function is another solution but it has the same drawbacks : you have to check manually every inputs, depending on the validation rule it can become quite tricky, plus you're losing one of the advantage of using template driven form instead of reactive ones.</p>