I need to dynamically add a validator to an angular form control from a
directive.
Example:
@Directive({ selector: 'my-directive' })
export class MyDirective implements AfterViewInit {
@Input() myDirective: boolean;
control: AbstractControl;
constructor(private form: NgForm, private el: ElementRef) {}
ngAfterViewInit() {
setTimeout(() => {
const name = this.el.nativeElement.getAttribute('name');
this.control = this.form.controls[name];
// this.control.setValidator... kills my other validators.
// also, how do I check if "required" or "minlength" are already applied?
});
}
}
How do I check if this control has, e.g. "required" attribute, and set it
if not? Without overriding other validators?
I could use Reactive forms extensions solving this, but the users of this
directive will be using template forms, so I want to avoid mandating much
in the components that use this.
--
You received this message because you are subscribed to the Google Groups
"Angular and AngularJS discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/angular.
For more options, visit https://groups.google.com/d/optout.