I have a template driven form which fills controls if data is available. But after the controls are filled, the error styles are not applied while validation(using Angular material elements). Error styles are applied only if I touch element or submit form. How can I trigger form validation manually ?
You can check stackblitz:
https://stackblitz.com/github/vugar005/Angular-NT-Components/tree/template-driven-approach
Thanks in advance
-
Please post an example. Most Angular form template examples only show the validation messages if the control has been touched or changed; your solution may be as simple as removing these conditions from the validation error message element but we will need an example.Timshel– Timshel2018-08-09 06:25:22 +00:00Commented Aug 9, 2018 at 6:25
-
added. please check .stackblitz.com/github/vugar005/Angular-NT-Components/tree/…Vugar Abdullayev– Vugar Abdullayev2018-08-09 06:47:24 +00:00Commented Aug 9, 2018 at 6:47
Add a comment
|
2 Answers
Trigger markAsTouched() method explicitly, if there is an error on input control during page load
getErrors(str) {
if (this.ntForm && this.ntForm.controls[str] ) {
const control = this.ntForm.controls[str];
const errors = control.errors;
if (!errors) { return; }
control.markAsTouched();
// remaining code
}
1 Comment
Vugar Abdullayev
Thanks. It worked ! I actually placed markAsTouched() after patching value of controls.