0

I have a reactive form and I want to show different error messages with <mat-error>, but I don't know how. I searched already a bit.

TS:

  selectDataFormGroup: FormGroup = this.formBuilder.group({
    headerCoordinate: ['', [
        Validators.required,
        Validators.pattern(/^.+:.+$/)
      ]
    ],
    contentCoordinate: ['', [
        Validators.required,
        Validators.pattern(/^.+:.+$/)
      ]
    ],
  });

HTML:

<mat-form-field fxFlex>
  <input matInput formControlName="headerCoordinate">
  <mat-error>{{ 'Field is required' | translate }}</mat-error>
</mat-form-field>

I need different error messages for required and pattern. <mat-error> shows up when the form field is not valid in general. How can I achieve this?

1 Answer 1

2

Could you try something like this :

<mat-form-field fxFlex>
  <input matInput formControlName="headerCoordinate">
  <mat-error *ngIf="selectDataFormGroup.get('headerCoordinate').hasError('required')">{{ 'Field is required' | translate }}</mat-error>
  <mat-error *ngIf="selectDataFormGroup.get('headerCoordinate').hasError('pattern')">{{ 'Field do not have the right format' | translate }}</mat-error>
</mat-form-field>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.