I want to implement custom validation (Required/Pattern/Disable) through directive in Angular 8:
<input type="text"
custom-data-annotation
[(ngModel)]="someValue"
name="userName" />
I have created the following directive:
@Directive({selector: '[custom-data-annotation]'})
export class CustomDataAnnotationDirective implements OnInit {
constructor(public el: ElementRef, private _renderer: Renderer2) {}
ngOnInit() {
this._renderer.setAttribute(this.el.nativeElement, 'required', 'true');
}
}
When I run the application and inspect element I can see the required attribute on input field, but when I submit my page without putting any value in that field it always returns false for "myForm.form.invalid"