0

I want to add some validation to the fields of a form through Validators in Angular2:

  • I'm binding ngForm to a form like this:

#form="ngForm"

  • For the fields i'm binding ngModel:

#field="ngModel", [(ngModel)]="model.field" type="text" name="field" placeholder="field"

The new form api suggests to use ngForm and ngModel, but there is no documentation in angular.io docs how to use Validators.

In the api reference to NgModel there are properties like validator : ValidatorFn and asyncValidator : AsyncValidatorFn

But i don't know how to reference the fields from the component's class to add the validators.

Hernan
  • 1,149
  • 1
  • 11
  • 29
  • 1
    http://learnangular2.com/forms/ – rashfmnb Aug 04 '16 at 20:16
  • what kind of validators you need? ng2 supports out of the box "required" "pattern", "min length" so you can just add them to your inputs, and #form would be invalid if one if your inputs is invalid – Amir Sasson Aug 04 '16 at 20:18
  • Yes, indeed. I've followed the link that @rashfmnb pointed, it's what i was looking for. – Hernan Aug 04 '16 at 20:40
  • I take that, the guide is outdated. [This one](http://blog.ng-book.com/the-ultimate-guide-to-forms-in-angular-2/) is for rc4. – Hernan Aug 05 '16 at 10:22

0 Answers0