Skip to content

Angular, Reactive form (FormControl, FormGroup, Validators)

2017-11-13 08:00:29

Simple example of Reactive forms. Everything what you need will be generated after executing this commands ng new hello-word to create new app and ng g c person-detail to create new component. After that, what you have to do, is adjust few files. Of course more you can find in Angular documentation, here we have only my notes. One more thing, if you don’t like to install node mess on your local computer you can use my or similar docker image From Angular Guide: 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...

What we want to buid

Angular Documentation

New app and component

By this two simple commants you will be able to create skeleton for you app. If you don't have already angular ci, you can use my docker image

ng new hello-world
ng g c person-detail

Bootstrap installation

You don't need but it will be good to install bootstrap, everything is described here -

Requred dependencies. FormsModule

You have to extend app.module.ts by adding ReactiveFormsModule (one new import and one new item in imports section)

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { ReactiveFormsModule } from '@angular/forms';

import { AppComponent } from './app.component';
import { HeroDetailComponent } from './hero-detail/hero-detail.component';
import { PersonDetailComponent } from './person-detail/person-detail.component';

  declarations: [
  imports: [
  providers: [],
  bootstrap: [AppComponent]
export class AppModule { }


import {Component} from '@angular/core';
import {FormControl, FormGroup, Validators} from '@angular/forms';

  selector: 'app-person-detail',
  templateUrl: './person-detail.component.html',
  styleUrls: ['./person-detail.component.css']
export class PersonDetailComponent {

  personForm = new FormGroup({

    name: new FormControl('', [
    email: new FormControl('email@email'),

    address: new FormGroup({
      city: new FormControl('Toruń'),
      street: new FormControl('Kopernika'),
      houseNumber: new FormControl('1473'),


  get name() {
    return this.personForm.get('name');



## Person Detail

<form [formgroup]='"personForm"' class='"well"' novalidate="">
<div class='"form-group"'>
<label class='"center-block"'>Name:
      <input class='"form-control"' formcontrolname='"name"'/>
<div &amp;&amp;="" (name.dirty="" *ngif='"name.invalid' alert-danger&quot;="" class='"alert' name.touched)&quot;="" ||="">
<div *ngif='"name.errors.required"'>Name is required.</div>
<div *ngif='"name.errors.minlength"'>Name must be at least 4 characters long.</div>
<div class='"form-group"'>
<label class='"center-block"'>Email:
      <input class='"form-control"' formcontrolname='"email"'/>
<section class='"well"' formgroupname='"address"'>
<div class='"form-group"'>
<label class='"center-block"'>City:
        <input class='"form-control"' formcontrolname='"city"'/>
<div class='"form-group"'>
<label class='"center-block"'>Street:
        <input class='"form-control"' formcontrolname='"street"'/>
<div class='"form-group"'>
<label class='"center-block"'>House Number:
        <input class='"form-control"' formcontrolname='"houseNumber"'/>

Form value: {{ personForm.value | json }}

Form status: {{ personForm.status | json }}

Add new component to screen (app.component.html)

<div class='"container"'>

Run test server

ng serve --watch true --poll 100 --host --port 4200 -o