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
Post a Comment