Angular 2 form validator for ionic 2


#1

Please refer to https://medium.com/@daviddentoom/angular-2-form-validation-9b26f73fcb81#.34i1pqpq5.

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


#2

Make it works finally by referring to this:


#3

Try this following lines before constructor

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

and in constructor

constructor(fb) {}