Articles in this section
Category / Section

Angular Reactive Forms

2 mins read

Angular reactive forms facilitate a reactive style of programming that favors explicit management of the data flowing between a non-UI data model (typically retrieved from a server) and a UI-oriented form model that retains the states and values of the HTML controls on screen. Reactive forms offer the ease of using reactive patterns, testing, and validation.

With reactive forms, you create a tree of Angular form control objects in the component class and bind them to native form control elements in the component template, using various techniques.

Syncfusion Angular Components supports reactive forms. To use Reactive Forms in our application, we must import ReactiveFormsModule in application’s root module.

Reactive Form API’s

FormControl are the basic building blocks of a reactive form or a form in general.

FormGroup is a collection/group of FormControls. It also offers many api’s such as tracking validity and values of the whole formGroup.

Refer to the below code snippet for view file.

<form class="form-horizontal" [formGroup]="form">

 

  <div class="form-group">

    <div class="col-sm-12">

      <label for="name" class="control-label required">Name</label>

      <input type="text" class="e-textbox" id="name" class="form-control" formControlName="name">

    </div>

  </div>

 

  <div class="form-group" >

    <div class="col-sm-12">

      <label for="email" class="control-label required">Email</label>

      <input type="text" class="e-textbox" id="email" class="form-control" formControlName="email">

    </div>

  </div>

  <div class="form-group" >

    <div class="col-sm-12">

      <label for="password" class="control-label required">Password</label>

      <input type="text" class="e-textbox" id="password" class="form-control" formControlName="password">

    </div>

  </div>

 

  <button class="btn btn-primary" ej-button (ejclick)="onSubmit()" text="Submit" [disabled]="!form.valid"></button>

  <button class="btn" ej-button (ejclick)="reset()" text="Reset"></button>

 

</form>

 

 

Refer to the below code snippet for model file of reactive form. In this form, the submit button will work only if the valid details will be given by the user.

import { Component, OnInit } from '@angular/core';

import { FormGroup, FormBuilder, Validators } from '@angular/forms';

 

@Component({

  selector: 'reactive-form',

  templateUrl: './reactiveform.component.html',

  styleUrls: ['./reactiveform.component.css']

})

export class ReactiveFormComponent implements OnInit {

    form: FormGroup;

    private formSumitAttempt: boolean;

   

    constructor(private formBuilder: FormBuilder) {}

 

    ngOnInit() {

      this.form = this.formBuilder.group({

        name: [null, Validators.required],

        email: [null, [Validators.required, Validators.email]],

        password: [null,Validators.required]

      });

    }

 

    onSubmit() {

      this.formSumitAttempt = true;

      if (this.form.valid) {

        console.log('form submitted');

        console.log(this.form.value)

      }

    }

 

    reset() {

      this.form.reset();

      this.formSumitAttempt = false;

    }

  }

 

 

Note:

We integrated the Reactive forms in our angular seed. Refer the seed here.

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied