<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: coding crossbow</title>
    <description>The latest articles on DEV Community by coding crossbow (@coding_crossbow).</description>
    <link>https://dev.to/coding_crossbow</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1174537%2Fecf55acb-7e8e-4227-a0fc-027c305a1c6d.png</url>
      <title>DEV Community: coding crossbow</title>
      <link>https://dev.to/coding_crossbow</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/coding_crossbow"/>
    <language>en</language>
    <item>
      <title>Template-driven and Reactive forms and validations in Angular</title>
      <dc:creator>coding crossbow</dc:creator>
      <pubDate>Thu, 20 Feb 2025 04:13:29 +0000</pubDate>
      <link>https://dev.to/coding_crossbow/template-driven-and-reactive-forms-and-validations-in-angular-1ocd</link>
      <guid>https://dev.to/coding_crossbow/template-driven-and-reactive-forms-and-validations-in-angular-1ocd</guid>
      <description>&lt;p&gt;Both Template-driven and Reactive forms in Angular support validations, but the way validations are implemented and managed differs between the two approaches. Below are examples of both, showcasing form validations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Template-driven forms with Validations&lt;/strong&gt;
In template-driven forms, validation is typically done directly in the template using Angular directives such as required, minlength, email, and others. You can also access form validity through the ngForm directive.
&lt;strong&gt;Example of Template-Driven Form with Validations:&lt;/strong&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- app.component.html --&amp;gt;
&amp;lt;form #userForm="ngForm" (ngSubmit)="onSubmit(userForm)"&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;label for="name"&amp;gt;Name&amp;lt;/label&amp;gt;
    &amp;lt;input 
      type="text" 
      id="name" 
      name="name" 
      ngModel 
      required 
      minlength="3"
      #name="ngModel"
    /&amp;gt;
    &amp;lt;div *ngIf="name.invalid &amp;amp;&amp;amp; name.touched"&amp;gt;
      &amp;lt;small *ngIf="name.errors?.required"&amp;gt;Name is required.&amp;lt;/small&amp;gt;
      &amp;lt;small *ngIf="name.errors?.minlength"&amp;gt;Name must be at least 3 characters long.&amp;lt;/small&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;div&amp;gt;
    &amp;lt;label for="email"&amp;gt;Email&amp;lt;/label&amp;gt;
    &amp;lt;input 
      type="email" 
      id="email" 
      name="email" 
      ngModel 
      required 
      email 
      #email="ngModel"
    /&amp;gt;
    &amp;lt;div *ngIf="email.invalid &amp;amp;&amp;amp; email.touched"&amp;gt;
      &amp;lt;small *ngIf="email.errors?.required"&amp;gt;Email is required.&amp;lt;/small&amp;gt;
      &amp;lt;small *ngIf="email.errors?.email"&amp;gt;Please enter a valid email address.&amp;lt;/small&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;button type="submit" [disabled]="!userForm.valid"&amp;gt;Submit&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;


// app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  onSubmit(form: any) {
    if (form.valid) {
      console.log('Form Submitted', form.value);
    } else {
      console.log('Form is invalid');
    }
  }
}

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Validation Breakdown:&lt;br&gt;
Required Validation: Ensures that the input field is not empty.&lt;br&gt;
Minlength Validation: Ensures that the name field has at least 3 characters.&lt;br&gt;
Email Validation: Ensures that the email is in a valid format.&lt;br&gt;
Touched State: Displays validation error messages only after the user has interacted with the field (touched).&lt;/p&gt;

&lt;p&gt;Pros of Template-Driven Validations:&lt;br&gt;
Declarative and Simple: You define validations directly in the template, making it quick and easy to set up.&lt;br&gt;
Automatic Binding: Angular takes care of binding form controls to the template, reducing boilerplate code in the component class.&lt;br&gt;
Cons:&lt;br&gt;
Limited Flexibility: Template-driven forms are best for simple forms. Complex logic or dynamic form behavior may require more effort.&lt;br&gt;
Harder to Manage Dynamic Validations: Handling dynamic or conditional validations can be challenging in template-driven forms.&lt;/p&gt;



&lt;p&gt;&lt;strong&gt;2. Reactive Forms with Validations&lt;/strong&gt;&lt;br&gt;
Reactive forms are more programmatic, and you define form controls, groups, and validations in the component class. This approach provides greater flexibility and control.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of Reactive Form with Validations:&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;&amp;lt;!-- app.component.html --&amp;gt;
&amp;lt;form [formGroup]="userForm" (ngSubmit)="onSubmit()"&amp;gt;
  &amp;lt;div&amp;gt;
    &amp;lt;label for="name"&amp;gt;Name&amp;lt;/label&amp;gt;
    &amp;lt;input 
      type="text" 
      id="name" 
      formControlName="name" 
    /&amp;gt;
    &amp;lt;div *ngIf="userForm.get('name')?.invalid &amp;amp;&amp;amp; userForm.get('name')?.touched"&amp;gt;
      &amp;lt;small *ngIf="userForm.get('name')?.hasError('required')"&amp;gt;Name is required.&amp;lt;/small&amp;gt;
      &amp;lt;small *ngIf="userForm.get('name')?.hasError('minlength')"&amp;gt;Name must be at least 3 characters long.&amp;lt;/small&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;div&amp;gt;
    &amp;lt;label for="email"&amp;gt;Email&amp;lt;/label&amp;gt;
    &amp;lt;input 
      type="email" 
      id="email" 
      formControlName="email" 
    /&amp;gt;
    &amp;lt;div *ngIf="userForm.get('email')?.invalid &amp;amp;&amp;amp; userForm.get('email')?.touched"&amp;gt;
      &amp;lt;small *ngIf="userForm.get('email')?.hasError('required')"&amp;gt;Email is required.&amp;lt;/small&amp;gt;
      &amp;lt;small *ngIf="userForm.get('email')?.hasError('email')"&amp;gt;Please enter a valid email address.&amp;lt;/small&amp;gt;
    &amp;lt;/div&amp;gt;
  &amp;lt;/div&amp;gt;

  &amp;lt;button type="submit" [disabled]="userForm.invalid"&amp;gt;Submit&amp;lt;/button&amp;gt;
&amp;lt;/form&amp;gt;


// app.component.ts
import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
})
export class AppComponent {
  userForm: FormGroup;

  constructor(private fb: FormBuilder) {
    this.userForm = this.fb.group({
      name: ['', [Validators.required, Validators.minLength(3)]],
      email: ['', [Validators.required, Validators.email]],
    });
  }

  onSubmit() {
    if (this.userForm.valid) {
      console.log('Form Submitted', this.userForm.value);
    } else {
      console.log('Form is invalid');
    }
  }
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Validation Breakdown:&lt;br&gt;
Required Validation: Ensures the name and email fields are not empty.&lt;br&gt;
Minlength Validation: Ensures that the name field has at least 3 characters.&lt;br&gt;
Email Validation: Ensures that the email field contains a valid email format.&lt;br&gt;
Touched State: Error messages are shown only if the form control is touched (i.e., the user interacts with the input).&lt;br&gt;
Pros of Reactive Forms Validations:&lt;br&gt;
Greater Control: You have full control over form data, validation logic, and form state.&lt;br&gt;
Scalability: Works well for large and complex forms. It’s easier to manage dynamic forms or conditional validations.&lt;br&gt;
Better for Unit Testing: Reactive forms are easier to unit test because the logic is defined in the component.&lt;br&gt;
Cons:&lt;br&gt;
More Boilerplate Code: You have to define the form group and validation logic in the component, which can make the setup more verbose.&lt;br&gt;
Less Declarative: Unlike template-driven forms, you need to define much of the form behavior in the TypeScript class.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;When to Use Each:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Template-Driven Forms: Best suited for simple forms with basic validation logic. It’s great when you want a quick, declarative setup.&lt;/li&gt;
&lt;li&gt;Reactive Forms: Ideal for more complex forms, dynamic forms, or when you need full control over form behavior, validation, and state management.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Both approaches support validations, and the choice between them depends on the complexity of your form and how much control you need over the form behavior.&lt;/p&gt;

</description>
      <category>angular</category>
      <category>programming</category>
      <category>javascript</category>
      <category>beginners</category>
    </item>
  </channel>
</rss>
