Angular 2 form validator for ionic 2

Please refer to Angular 2 Form Validation. DISCLAIMER This example is not up to… | by David Den Toom | Medium.

But I have a problem to implement angular 2 form validator in my ionic 2 project. Here is what I did:

  • Create an ionic application

ionic start formValidator blank --v2

  • In app/pages/home/home.js:

      import {Page} from 'ionic/ionic';
      import { FormBuilder,Validators,Control,ControlGroup,FORM_DIRECTIVES
      } from 'angular2/common';
      import {UsernameValidator} from '../../usernameValidator';
    
      @Page({
          templateUrl: 'build/pages/home/home.html',
          directives: [FORM_DIRECTIVES]
      })
    
  • In app/pages/home/home.html:

       <form [ngFormModel]="form">
    

I got the following error message:

EXCEPTION: TypeError: Cannot read property ‘validator’ of undefined in [form in HomePage@7:8]

Do I miss anything? Thanks

Make it works finally by referring to this:

3 Likes

Try this following lines before constructor

 static get parameters() {
        return [
            [FormBuilder]
        ];
    }

and in constructor

constructor(fb) {}