No Provider for FormBuilder

Angular

Angular Problem Overview


I'm still new to this and learning via tutorials, but for some reason i'm getting the message No provider for formbuilder when trying to test unique username. I'm not sure why i'm getting this message, but i'm unable to find a solution. Can anyone tell me why this is happening?

signup-component.ts

import {Component} from '@angular/core';
import {ControlGroup, Control, Validators, FormBuilder} from '@angular/common'
import {UsernameValidators} from './usernameValidators'

@Component({
selector: 'signup',
templateUrl: 'signup-component.html'

})
export class SignupComponent{
  form: ControlGroup;

  constructor(fb: FormBuilder){
    this.form = fb.group({
      username:['', Validators.compose([
          Validators.required, UsernameValidators.cannotContainSpace
      ])],
      password: ['', Validators.required]
    })
  }
}

usernameValidators.ts

import {Control} from '@angular/common'


export class UsernameValidators{

  static shouldBeUnique(control: Control){
    return new Promise((resolve, reject) => {
      setTimeout(function(){
        if(control.value == "andy")
          resolve({shouldBeUnique: true});
        else
          resolve(null);
      }, 1000);

    });

  }


  static cannotContainSpace(control: Control){
    if (control.value.indexOf(' ') >= 0)
      return {cannotContainSpace: true};

    return null;
  }
}

Angular Solutions


Solution 1 - Angular

Import ReactiveFormsModule and FormsModule

@NgModule({
  imports: [
      BrowserModule /* or CommonModule */, 
      FormsModule, ReactiveFormsModule
  ],
  ...
})

in the module where you're using FormBuilder

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionuser6680View Question on Stackoverflow
Solution 1 - AngularGünter ZöchbauerView Answer on Stackoverflow