Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Material - show mat-error on button click

I am trying to do validation using the <mat-form-field> and <mat-error>. This works fine when user tabs out of the input without filling. But how do I force this error to show when I click a button? I am not using submit. Also, using template-driven forms.

This is my code:

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]);

like image 452
ganeshk Avatar asked Oct 14 '17 13:10

ganeshk


People also ask

How do you show error in mat form field?

Error messages can be shown under the form field underline by adding mat-error elements inside the form field. Errors are hidden initially and will be displayed on invalid form fields after the user has interacted with the element or the parent form has been submitted.

What is MatFormFieldControl?

MatFormFieldControl. An interface which allows a control to work inside of a MatFormField .

What is MatInput?

MatInput. Directive that allows a native input to work inside a MatFormField .


2 Answers

See how to use a form with a custom ErrorStateMatcher

If you wish to override this behavior (e.g. to show the error as soon as the invalid control is dirty or when a parent form group is invalid), you can use the errorStateMatcher property of the matInput. The property takes an instance of an ErrorStateMatcher object. An ErrorStateMatcher must implement a single method isErrorState which takes the FormControl for this matInput as well as the parent form and returns a boolean indicating whether errors should be shown. (true indicating that they should be shown, and false indicating that they should not.)

I would make a separate file such as default.error-matcher.ts

/** Error when invalid control is dirty or touched*/ export class MyErrorStateMatcher implements ErrorStateMatcher {   isErrorState(control: FormControl | null, form: FormGroupDirective | NgForm | null): boolean {     return !!(control && control.invalid && (control.dirty || control.touched));   } } 

Then in the TS file add:

matcher = new MyErrorStateMatcher(); 

Then change the input to use matcher:

<mat-form-field>     <input matInput placeholder="Due Date" name="dueDate" [(ngModel)]="dueDate" [formControl]="dueDateValidator" [errorStateMatcher]="matcher" required>     <mat-error *ngIf="dueDateValidator.invalid">Due Date is required for Tasks</mat-error> </mat-form-field> 
like image 156
Kyle Pfromer Avatar answered Sep 19 '22 03:09

Kyle Pfromer


Since you want to show mat error on button's click, please try the below: For Angular6 version:

  1. Import the required classes
import { FormControl, FormBuilder, FormGroup } from '@angular/forms'; 
  1. Declare form control in your component.ts file:
nameControl = new FormControl(''); 
  1. Add the control in html:
<mat-form-field  style="width: 100%" floatPlaceholder="never">     <input matInput placeholder="your placeholder text" [formControl]="nameControl" required/>     <mat-error *ngIf="nameControl.errors?.required">name is required</mat-error> </mat-form-field> 
  1. Add this to your button click event handler:
this.nameControl.markAsTouched(); 

It's important to check how you are using the form control, ".markAsTouched()" on point 4 will show the mat error for the corresponding form control.

like image 31
Simran kaur Avatar answered Sep 19 '22 03:09

Simran kaur