Edit (May, 4 2017):
After a lot of research, it's clear to me that this is not currently possible in a "native" way. See here: https://github.com/angular/angular/issues/7113
Original Post:
Currently, the following code allows me to display a validation error when the user clicks the submit button without entering a valid email into the input field:
import {
Component
} from '@angular/core';
@Component({
selector: 'app-login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent {
submitted = false;
public submitForm(form: any): void {
this.submitted = true;
console.log(form);
}
}
.invalid-message {
color: yellow;
}
<form id="loginForm" #loginForm="ngForm" (ngSubmit)="submitForm(loginForm.value)">
<div class="form-group">
<input type="email" class="form-control form-control-lg" #email="ngModel" name="email" ngModel required pattern="^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$" placeholder="email">
<ng-container *ngIf="this.submitted && email.errors">
<small [hidden]="!email.errors.required" class="invalid-message float-right">EMAIL REQUIRED</small>
<small [hidden]="!email.errors.pattern" class="invalid-message float-right">INCORRECT EMAIL FORMAT</small>
</ng-container>
</div>
<button type="submit" form="loginForm" class="btn btn-secondary btn-block btn-lg">LOGIN</button>
</form>
What I would like to have is the validation error message disappearing automatically when the user starts typing again anything into the input. This is the Airbnb behavior for example, that you can experience if you try to login on the web.