The form in this example (built with FormBuilder) is reactive form
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
imports: [ ..., FormsModule, ReactiveFormsModule ],
I'm not sure if all these imports (FormControl
, FormGroup
, FormBuilder
, Validators
and Validators
) are necessary. I built this example along tutorial on udemy. But this tutorial worked with a beta release of Angular 2 and just before my self study came the 2.0.0 out. There were braking changes and I had to figure out what works with the the form builder. In the video tutorial they used Control
, ControlGroup
, FormBuilder
, Validators
, FORM_DIRECTIVES
, ngControl
and [ngFormModel]
.
import { Component } from '@angular/core';
import { FormControl,
FormGroup,
FormBuilder,
Validators } from '@angular/forms';
import { EmailValidator } from './emailValidator';
Use the FormGroup from @angular/forms
<form [formGroup]="form">
first a varibale for the form
form: FormGroup;
and then in the constructor
this.form = builder.group({
...
});
With FormControl form @angular/forms
<input type="text" class="form-control" [formControl]="form.controls['username']" >
The variables for the form fields
username: FormControl;
email: FormControl;
And in the constructor for the validation
this.form = builder.group({
'username': [the validation parameters],
'email': [the validation parameters]
});
<div *ngIf="form.controls['username'].dirty && !form.controls['username'].pending && !form.controls['username'].valid">
<div *ngIf="!form.controls['username'].required" class="alert alert-danger">Username required</div>
<div *ngIf="!form.controls['username'].minLength" class="alert alert-danger">Minimum lengt: 3 characters</div>
</div>
this.form = builder.group({
'username': [null, Validators.compose([Validators.required, Validators.minLength(3)])],
'email': [null, Validators.compose([Validators.required, EmailValidator.invalidEmail])]
});
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^
the validation
Note the regex pattern. There are thousand's different version around, but this works well.
import { FormControl } from '@angular/forms';
interface ValidationResult {
[key: string]: boolean;
}
export class EmailValidator {
static invalidEmail(control: FormControl) {
let pattern = /[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9]*[a-z0-9])?/;
if ( pattern.test( control.value ) ) {
return null;
} else {
return {'invalidEmail': true};
};
}
}