forms - Angular2 How to add Validator to FormControl after control is created? -


we have component has dynamically built form. code add control validators might this

var c = new formcontrol('', validators.required); 

but let's want add 2nd validator later. how can accomplish this? cannot find documentation on online. did find though in form controls there setvalidators

this.form.controls["firstname"].setvalidators  

but not clear how add new or custom validator.

thanks bunch.

you pass formcontrol array of validators.

here's example showing how can add validators existing formcontrol:

this.form.controls["firstname"].setvalidators([validators.minlength(1), validators.maxlength(30)]); 

note, reset existing validators added when created formcontrol.


Comments

Popular posts from this blog

javascript - Replicate keyboard event with html button -

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Web audio api 5.1 surround example not working in firefox -