Reputation: 19278
I wrote a custom control component and I want to get which validators are set on it. I want te get which validators are set so I can add a *
for a required field for example. I am using template driven form.
Is this possible and how?
My Component:
@Component({
selector: 'input-text2',
templateUrl: './input-text2.component.html',
styleUrls: ['/input-base2.scss', './input-text2.component.scss'],
providers: [
{ provide: NG_VALUE_ACCESSOR, multi: true, useExisting: InputText2Component }
]
})
export class InputText2Component extends InputBase2 implements ControlValueAccessor, OnInit {
private ngControl: NgControl;
value: string;
valueChange: (value: any) => void;
_onTouched: (value: any) => void;
constructor(private injector: Injector) {
super();
}
ngOnInit(): void {
this.ngControl = this.injector.get(NgControl);
}
..//other methods
}
Upvotes: 2
Views: 874
Reputation: 657308
I think adding these constructor dependencies to your component should get you the validators.
@NgModule({
imports: [ BrowserModule, FormsModule, ReactiveFormsModule ],
constructor(@Optional() @Inject(NG_VALIDATORS) private validators,
@Optional() @Inject(NG_ASYNC_VALIDATORS) private validators) {}
The FormsModule
should export these tokens.
Upvotes: 1