I am struggling with validation using the <mat-form-field>
and <mat-error>
. The validation works as expected when the user tabs out of the input without filling it. However, I'm facing a challenge in making this error show up when a button is clicked. I want to trigger the error display without using submit. Additionally, I am utilizing template-driven forms.
Here's the code snippet:
HTML:
<mat-form-field>
<input matInput placeholder="Due Date" name="dueDate" [(ngModel)]="dueDate" [formControl]="dueDateValidator" required>
<mat-error *ngIf="dueDateValidator.invalid">Due Date is required for Tasks</mat-error>
</mat-form-field>
TS:
dueDateValidator: FormControl = new FormControl('', [Validators.required]);