<mat-form-field>
<mat-label></mat-label>
<input [required] = "isMandatory? true:false" placeholder="Just a placeholder">
<mat-error *ngIf="isMandatory">mandatory field</mat-error>
</mat-form-field>
The above code is working fine and showing the error message when the textbox gets dirty or in focus. Now, I am trying to show before hand when the page loads initially.
<input [required] = "isMandatory? true:false" placeholder="Just a placeholder"
ng-model-options="{ updateOn: 'change blur' }">
Tried above approach with no luck. Is there any way to show error messages when form initially loads? Thanks!
One way to achieve this would be to add the following code inside ngOnInit():
ngOnInit(): void {
// FormControl or FormGroup initialization code
// ....................................
this.yourControl.markAsTouched();
// OR, if you want for the whole formGroup (and your're using at leat Angular 8)
this.yourFormGroup.markAllAsTouched();
}
This should do the trick and show the client side errors (required in your case) upfront.