O'Reilly logo

Switching to Angular - Third Edition by Minko Gechev

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Using composition of control validators

We took a look at how we can apply a single validator to form controls. Using the model-driven approach, we applied the Validators.required validator in a way equivalent to what we did in the previous chapter, where we used template-driven forms and added the required attribute. However, in some applications, the domain may require a more complex validation logic. For example, if we want to apply both the required and the validateEmail validators to the email control, we should do the following:

this.addDevForm = fb.group({ 
  ... 
  email: ['', Validators.compose([ 
      Validators.required, 
      validateEmail    ])  ], 
  ... 
}); 

The compose method of the Validators object accepts an array of validators as an argument and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required