Common validations

Before we move on, we need to implement validations for loginForm. As we implement more forms in Chapter 10, Angular App Design and Recipes, you will realize that it gets tedious, fast, to repeatedly type out form validations in either template or reactive forms. Part of the allure of reactive forms is that it is driven by code, so we can easily extract out the validations to a shared class, unit test, and reuse them:

  1. Create a validations.ts file under the common folder
  2. Implement email and password validations:
src/app/common/validations.tsimport { Validators } from '@angular/forms'export const EmailValidation = [Validators.required, Validators.email]export const PasswordValidation = [  Validators.required, Validators.minLength(8), ...

Get Angular 6 for Enterprise-Ready Web Applications now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.